How to Read a Newline in C
05-28-2002 #1
Registered User
Reading information from a file till in that location is a newline?
this is what i am trying but it aint workingfptr is a file pointer and counter is counting the number of times there is a newline.Code:
while (fptr == '\due north') counter++;why is it wrong? and what would be the right code otherwise
tin someone plz help
cheers
05-28-2002 #2
Unleashed
The world is waiting. I must leave you now.
05-28-2002 #4
Terminate Of Line
It does, but only for files that have lines <81 chars long. If you want to guarantee that you're counting all the newlines chars, you'd probably best check each character.i recall this does the chore
Hither's an extract from my manual for fgets():
A common programming mistake is to assume the presence of a new-line character in every string that is read into the array. A new-line character will not be present when more northward-1 characters occur before the new-line. Also, a new-line character might non appear as the last character in a file, just before cease-of-file.
When all else fails, read the instructions.
If y'all're posting code, employ lawmaking tags: [code] /* insert lawmaking here */ [/code]
05-28-2002 #7
Registered User
Another variation is to use the post-obit, skilful for counting the number of variable size records, each terminated by a new line character, on a text file.the fscanf volition read the data upwardly to the new line character and the side by side "\n" takes reads the newline at the end of the record.Lawmaking:
#define MAX_LEN /*whatsoever yous think appropriate */ char str[MAX_LEN]; fscanf("%[^\n]\n", str); while(str != EOF) { counter++; fscanf("%[^\due north]\n", str); }
hoping to exist certified (programming in c)
hither'south the news - I'm officially certified.
05-28-2002 #8
Cease Of Line
Did you compile that, 'cos I couldn't. fscanf() is defined asOriginally posted past bigtamscot
Another variation is to use the following, good for counting the number of variable size records, each terminated by a new line grapheme, on a text file.
<-- snip code -->
the fscanf will read the information upwardly to the new line grapheme and the adjacent "\n" takes reads the newline at the end of the tape.
You lot are missing some parms. Too, fscanf render EOF, it doesn't identify it into one of its parameter variables.int fscanf( FILE *fp, const char *format, ... );
Even when I fixed these $.25, information technology still didn't requite the right answer, so I'chiliad not sure the "%[^\north]\n" bit is correct.
When all else fails, read the instructions.
If yous're posting code, use code tags: [code] /* insert code hither */ [/code]
05-28-2002 #9
Code Goddess
str is a reserved proper name, fscanf doesn't accept the correct arguments, str will never exist EOF, and the whole %[^\n]\northward format is just nasty. Even with the errors stock-still this lawmaking wouldn't give the right output, peradventure something more than like this:Code:
#define MAX_LEN /*whatever you lot think appropriate */ char str[MAX_LEN]; fscanf("%[^\n]\northward", str); while(str != EOF) { counter++; fscanf("%[^\n]\n", str); }
-PreludeCode:
#define MAX_LEN /*whatever you lot retrieve advisable */ char fstr[MAX_LEN]; while ( fgets ( fstr, sizeof fstr, file ) != Nothing ) counter++;
My best code is written with the delete key.
05-28-2002 #10
Registered User
1, no i never did compile it.
ii, deplorable i forgot the file stream in the parameters.
3, I have never had any problems with it, although i would never utilize information technology where i was not sure whether the len of string would get out of bounds.Deplorable guys a quick idea when i was at work and posted in a hurry.
hoping to exist certified (programming in c)
here'south the news - I'm officially certified.
05-28-2002 #eleven
Registered User
Okay folks compiled my code above and information technology crashed severely. Information technology needed a few alterations. I donot know if it is more efficient than the other methods shown, but information technology does work, every bit long as the length of the maximum line on the file is no longer than MAXSIZE.Lawmaking:
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 150 /*OR ANY LEN YOU NEED*/ int main (void) { char str[150]; FILE *infp; int count = 0; if((infp = fopen("myfile.dat", "rt"))== Zippo) { printf("\nE R R O R : Unable to open input file"); exit (1); } printf("\nNow counting number of records on file"); while((*str = fgetc(infp))!= EOF) { fscanf(infp,"%[^\n]\n", str); count++; } printf("\n\nNumber of records on file is %d", count); getchar(); fclose(infp); return 0; }
hoping to exist certified (programming in c)
here'southward the news - I'm officially certified.
05-28-2002 #12
End Of Line
Have another read of Preludes comments on your lawmaking....
When all else fails, read the instructions.
If you're posting code, employ code tags: [code] /* insert code here */ [/code]
06-27-2002 #fifteen
ATH0
Um... no. This is entirely incorrect. What is this even supposed to practise? First off, 'str' is an array, and y'all're dereferencing information technology ... and then y'all endeavour to use .... yeah. Anyway information technology's wrong.while((*str = fgetc(infp))!= EOF) {
if( strch(str, '\n') )
count++;
}Quzah.
Hope is the first step on the road to disappointment.
Source: https://cboard.cprogramming.com/c-programming/18628-reading-data-file-till-there-newline.html