Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4612448
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:24:15+00:00 2026-05-22T01:24:15+00:00

Say I need to read in two name like, [name name]\n …. (possibly more

  • 0

Say I need to read in two name
like, [name name]\n …. (possibly
more [name name]\n . Assuming the name
can have length of 19, my code so far
is, How would I actually prevent an input like [name name name]\n or more [name name name...]\n in my case ? I heard about fgets() and fscanf but would anyone kindly show me an example how to use them? Thanks in advance.

char name1[20];
char name2[20];
for(int i=0; i < numberOfRow ; i++){
  scanf(" %s %s", name1, name2);
}

Ok So I found a way to make sure there is only two element, but I am not sure how to put them back into variable…

char str[50];
int i;
int count = 0;
fgets(str, 50, stdin);

i = strlen(str)-1;
for(int x=0; x < i ;x++){
  if(isspace(str[x]))
    count++;
}
if(counter > 1){
  printf("Error: More than 2 elements.\n");
}else if{
//How do i place those two element back into the variable ?
char name1[20];
char name2[20];

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:24:16+00:00Added an answer on May 22, 2026 at 1:24 am

    You can use strtok (string.h). Please be careful, this function will modify your source string (you may copy the string before).

    Example for strtok:

    char* word;
    
    // First word:
    word = strtok(str, " "); // space as the delimiter
    strncpy(name1, word, sizeof(name1) - 1); 
    name1[sizeof(name1) - 1] = 0;  // end of word, in case the word size is > sizeof(name1)    
    
    // Second word
    word = strtok (NULL, " ");
    strncpy(name2, word, sizeof(name2) - 1);
    name2[sizeof(name2) - 1] = 0;
    

    Also, I think you should chec

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say, hypothetically (read: I don't think I actually need this, but I am
Let's say I need to use Python and C++. I can call Python function
Lets say I have two separate databases, X and Y, on the same physical
I have a peculiar problem. I need to read (from a txt file) using
I have two distinct (unknown relationship) types of input patterns and I need to
I have two files, say source and target. I compare each element in source
DESCRIPTION I have two datasets with information that I need to merge. The only
I have a file which contains several text entries, say one two three Now,
I have need to use one of two custom file readers classes; one to
Say if I have a controller, profile, which has just two actions. The first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.