0

I read string from file if the string in the file is:

 "User1 User2 User3 User4 User5"
 "test1"

i want to read only the first line from the file,my code is:

 if (in_fp != NULL)
 {
   while (fscanf(in_fp, "%s", &user)!=EOF)
    {
      temp = (char*)malloc(sizeof user);
      strcpy(temp,user);
     }
 fclose (in_fp);
 }

but the problem is that fscanf read also that "test1" from the text file,i need to know how to make him to know that is the end of the line,what i need to fix in my code? i know about fgets but i want to read each user separately

user2976686
  • 159
  • 1
  • 1
  • 8

3 Answers3

6

a way

    if (in_fp != NULL){
        while (fscanf(in_fp, "%s%c", user, &ch)==2){
            temp = (char*)malloc(sizeof user);
            strcpy(temp, user);
            //do something
            printf("%s\n", user);
            free(temp);
            if(ch == '\n')
                break;
        }
        fclose (in_fp);
    }
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
2

To use fscanf("%s") and variant formats is a tough way to solve this task.

The %s consumes leading whitespace including ' ' and '\n' indiscriminately, yet OP uses these 2 distinctly as a username delimiter and record delimiter. Much better to read a line, using fgets() and then parse the buffer read.

OP is concerned about about unknown line length. Whereas it is good to not limit code to only work with a short line, designing code to work with an endless line has a problem too. Excessively long lines are increasing a sign of errant data or nefarious attempts to hack. Better to allocate a big buffer and gracefully complain should a data file try to exceed it.

size_t N = 4096;
char *buffer = malloc(N);
if (buffer == NULL) ...
while (fgets(buffer, N, in_fp) != NULL) {
  size_t Len = strlen(buffer);
  if (Len == 0 || buffer[Len-1] != '\n) {
    HandleUnacceptbaleBuffer();
  }
  // parse buffer via sscanf, strtok, etc.
  char *p = buffer;
  int n
  while (1 == sscanf(p, "%s%n", &user, &n)) {
    Handle(user);
    p += n;
  }
}
free (buffer);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1

You don't.

And you don't want to use scanf() either. Avoid it like the plague.

If you want to read lines, then use fgets(). (Use it even if you don't want to read lines, still much better than scanf()).


I see the "but I want to read each word separately" question incoming. So, here you are:

char buf[LINE_MAX];
if (!fgets(buf, sizeof buf, stdin)) {
    // handle error
}

char *p = buf, *s = p;
while (p) {
    p = strtok_r(s, " ", &s);
    // do stuff with `p` if it's not NULL
}

Also, do NOT cast the return value of malloc()!

Community
  • 1
  • 1
  • thanks about your link. i dont know the maximum length of the first line,thats why i use in fscanf,i need to read from the file and to stop when its the second line in the text thats what i need know – user2976686 Dec 21 '13 at 16:58
  • @user2976686 If the line is longer than `MAX_LINE` (which theoretically can't be the case), then either use `fread()` to read the entire file into memory and replace the first `'\n'` with a NUL character, or use dynamic memory allocation, exponential storage expansion and `fgetc()`. –  Dec 21 '13 at 17:00
  • ,hi,my problem is that i DONT KNOW what is the length of maximun line! – user2976686 Dec 21 '13 at 17:04
  • sorry but i still dont understand your answer :( – user2976686 Dec 21 '13 at 17:05
  • getline() also works, and allocates the buffer for you. – Demi Dec 21 '13 at 17:56
  • @Demetri But that's not the reason for downvoting this (correct) answer. And `getline()` is not in the C standard library. –  Dec 21 '13 at 18:01
  • i don't did downvoting to the answer. – user2976686 Dec 21 '13 at 18:11
  • 1
    @H2CO3 getline() is specified by POSIX and is part of glibc though – Demi Dec 22 '13 at 03:51