Read Until End of Line C Read File
05-28-2002 #1
Registered User
Reading information from a file till at that place is a newline?
this is what i am trying just it aint workingfptr is a file arrow and counter is counting the number of times there is a newline.Code:
while (fptr == '\n') counter++;why is it wrong? and what would be the right code otherwise
tin someone plz assistance
cheers
05-28-2002 #2
Unleashed
The world is waiting. I must leave you now.
05-28-2002 #iv
End Of Line
It does, but but for files that have lines <81 chars long. If you desire to guarantee that you're counting all the newlines chars, you'd probably all-time cheque each character.i recall this does the task
Here's an extract from my transmission for fgets():
A common programming error is to assume the presence of a new-line character in every cord that is read into the array. A new-line graphic symbol will not be present when more than than north-1 characters occur before the new-line. Also, a new-line graphic symbol might non announced as the terminal character in a file, merely before end-of-file.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code hither */ [/code]
05-28-2002 #7
Registered User
Another variation is to use the following, adept for counting the number of variable size records, each terminated by a new line character, on a text file.the fscanf will read the data up to the new line grapheme and the next "\north" takes reads the newline at the cease of the tape.Lawmaking:
#define MAX_LEN /*whatever you think appropriate */ char str[MAX_LEN]; fscanf("%[^\n]\n", str); while(str != EOF) { counter++; fscanf("%[^\n]\n", str); }
hoping to be certified (programming in c)
here'south the news - I'm officially certified.
05-28-2002 #viii
End Of Line
Did you compile that, 'cos I couldn't. fscanf() is defined asOriginally posted by bigtamscot
Some other variation is to use the post-obit, expert for counting the number of variable size records, each terminated by a new line grapheme, on a text file.
<-- snip lawmaking -->
the fscanf will read the data upward to the new line character and the next "\north" takes reads the newline at the cease of the record.
You are missing some parms. Besides, fscanf return EOF, it doesn't identify it into i of its parameter variables.int fscanf( FILE *fp, const char *format, ... );
Even when I stock-still these bits, information technology nevertheless didn't give the correct answer, so I'1000 not sure the "%[^\northward]\n" bit is correct.
When all else fails, read the instructions.
If you're posting code, utilise code tags: [code] /* insert lawmaking hither */ [/code]
05-28-2002 #9
Code Goddess
str is a reserved name, fscanf doesn't have the correct arguments, str will never be EOF, and the whole %[^\n]\n format is just nasty. Fifty-fifty with the errors fixed this code wouldn't give the correct output, possibly something more like this:Code:
#define MAX_LEN /*any you retrieve appropriate */ char str[MAX_LEN]; fscanf("%[^\n]\n", str); while(str != EOF) { counter++; fscanf("%[^\due north]\due north", str); }
-PreludeCode:
#define MAX_LEN /*whatever you remember appropriate */ char fstr[MAX_LEN]; while ( fgets ( fstr, sizeof fstr, file ) != Nada ) counter++;
My best lawmaking is written with the delete primal.
05-28-2002 #10
Registered User
1, no i never did compile it.
2, sorry i forgot the file stream in the parameters.
3, I have never had whatsoever bug with it, although i would never use it where i was not sure whether the len of string would get out of bounds.Lamentable guys a quick thought when i was at work and posted in a hurry.
hoping to be certified (programming in c)
here's the news - I'm officially certified.
05-28-2002 #11
Registered User
Okay folks compiled my code to a higher place and information technology crashed severely. It needed a few alterations. I donot know if it is more efficient than the other methods shown, simply it does work, as long as the length of the maximum line on the file is no longer than MAXSIZE.Code:
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 150 /*OR ANY LEN YOU NEED*/ int chief (void) { char str[150]; FILE *infp; int count = 0; if((infp = fopen("myfile.dat", "rt"))== NULL) { printf("\nE R R O R : Unable to open input file"); exit (ane); } printf("\nNow counting number of records on file"); while((*str = fgetc(infp))!= EOF) { fscanf(infp,"%[^\n]\n", str); count++; } printf("\northward\nNumber of records on file is %d", count); getchar(); fclose(infp); render 0; }
hoping to be 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 code....
When all else fails, read the instructions.
If you're posting code, use code tags: [lawmaking] /* insert code here */ [/code]
06-27-2002 #xv
ATH0
Um... no. This is entirely wrong. What is this even supposed to do? Starting time off, 'str' is an array, and you're dereferencing it ... and then you try to utilize .... aye. Anyway information technology's wrong.while((*str = fgetc(infp))!= EOF) {
if( strch(str, '\northward') )
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
0 Response to "Read Until End of Line C Read File"
Post a Comment