3

I have a file with multiple strings, each string on a separate line. All strings are 32 character long (so 33 with the '\n' at the end).

I am trying to read all the strings. For now, I just want to read them and not store them as follows:

char line[32];
while (!feof(fp)) {
    fgets(line, 32, fp);
}
printf("%s", line);

This prints out zero. Why isn't it working?

Furthermore, I am trying to store a null terminator at the end of each string read. I changed the line array to length 33 but how would I make it that if '\n' is found, replace it with \0 and store that?

1
  • As far as why your example isn't working -- it is for 2 reasons, first, the strings are non-NUL terminated, second, you printed after ending the while loop so it would, at best, only print the last line. Commented Nov 14, 2011 at 22:35

3 Answers 3

4

You code isn't working because you are only allocating space for lines of 30 characters plus a newline and a null terminator, and because you are only printing out one line after feof() returns true.

Additionally, feof() returns true only after you have tried and failed to read past the end of file. This means that while (!feof(fp)) is generally incorrect - you should simply read until the reading function fails - at that point you can use feof() / ferror() to distinguish between end-of-file and other types of failures (if you need to). So, you code could look like:

char line[34];

while (fgets(line, 34, fp) != NULL) {
    printf("%s", line);
}

If you wish to find the first '\n' character in line, and replace it with '\0', you can use strchr() from <string.h>:

char *p;

p = strchr(line, '\n');
if (p != NULL)
    *p = '\0';
Sign up to request clarification or add additional context in comments.

4 Comments

Since fgets automatically adds a null terminator, is there a way to "delete" the \n instead of switching it to a null terminator and having two null-terminators? If not, what are the "consequences" of two null terminators?
@Nayefc: Not really - the only way to "delete" it would be to copy the string without it to a new, smaller array - but there is very little point to doing so. There's no consequences to having two null-terminators - everything after the first null-terminator is simply considered junk that isn't part of the string.
I think, i almost did the same mistake,...just want to clarify it : here the mistake was the condition while(!feof(fp)) whereas i inserted while(getchar!==EOF)......were our mistakes same or different ? but my code printed only the first line present in the file and from thereon it printed the next line only on pressing enter.
@ArnavDas: Yes, that is the same as one of the mistakes in the original question here. while (getchar() != EOF) is the right way to go.
1

Here is a basic approach:

// create an line array of length 33 (32 characters plus space for the terminating \0)
char line[33];
// read the lines from the file
while (!feof(fp)) {
    // put the first 32 characters of the line into 'line'
    fgets(line, 32, fp);
    // put a '\0' at the end to terminate the string
    line[32] = '\0';
    // print the result
    printf("%s\n", line);
}

4 Comments

Another way to do this, with same basic structure, is to use fscanf(fp,"%s",line);, this way you don't need to know the size of the string ahead of time, just simply have an array big enough.
@iKiar: This way if there are whitespace character in the line it would be a disaster...
That's true, thanks for pointing out. Only works without whitespace, like single words, or a combination of numbers and letters, etc. Thanks
while (!feof()) { } is generally not the right thing to do. EOF is only detected after an attempt is made to read at the end of file - so, for example, this code will print the last line twice.
0

It goes something like this:

char str[33]; //Remember the NULL terminator
while(!feof(fp)) {
  fgets(str, 33, fp);
  printf("%s\n",str);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.