How to Read a File and Split It by Newlilne in C
05-28-2002 #1
Registered User
Reading data from a file till there is a newline?
this is what i am trying merely information technology 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
![]()
can someone plz help
![]()
thanks
05-28-2002 #2
Unleashed
The world is waiting. I must leave you now.
05-28-2002 #four
End Of Line
It does, simply only for files that accept lines <81 chars long. If yous desire to guarantee that you're counting all the newlines chars, you lot'd probably all-time cheque each character.i think this does the chore
Here'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 graphic symbol will not be present when more than n-i characters occur earlier the new-line. As well, a new-line character might not appear as the last graphic symbol in a file, just before end-of-file.
When all else fails, read the instructions.
If you're posting code, use code tags: [lawmaking] /* insert code here */ [/lawmaking]
05-28-2002 #seven
Registered User
Another variation is to use the post-obit, expert for counting the number of variable size records, each terminated by a new line graphic symbol, on a text file.the fscanf volition read the information upward to the new line grapheme and the next "\n" takes reads the newline at the end of the record.Lawmaking:
#define MAX_LEN /*whatsoever you think appropriate */ char str[MAX_LEN]; fscanf("%[^\due north]\n", str); while(str != EOF) { counter++; fscanf("%[^\n]\n", str); }
hoping to be certified (programming in c)
here'southward the news - I'm officially certified.
05-28-2002 #8
Terminate Of Line
Did you compile that, 'cos I couldn't. fscanf() is defined asOriginally posted by 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 up to the new line character and the next "\n" takes reads the newline at the end of the tape.
Yous are missing some parms. Besides, fscanf return EOF, it doesn't place it into ane of its parameter variables.int fscanf( FILE *fp, const char *format, ... );
Even when I fixed these bits, it still didn't give the right reply, so I'1000 non sure the "%[^\n]\due north" bit is correct.
When all else fails, read the instructions.
If you lot're posting code, employ code tags: [lawmaking] /* insert lawmaking here */ [/code]
05-28-2002 #9
Code Goddess
str is a reserved name, fscanf doesn't have the right arguments, str volition never be EOF, and the whole %[^\n]\n format is only nasty. Even with the errors fixed this code wouldn't give the correct output, possibly something more like this:Code:
#ascertain MAX_LEN /*whatever you recollect appropriate */ char str[MAX_LEN]; fscanf("%[^\n]\n", str); while(str != EOF) { counter++; fscanf("%[^\n]\n", str); }
-PreludeCode:
#define MAX_LEN /*any you lot retrieve appropriate */ char fstr[MAX_LEN]; while ( fgets ( fstr, sizeof fstr, file ) != NULL ) counter++;
My best lawmaking is written with the delete cardinal.
05-28-2002 #10
Registered User
i, no i never did compile it.
2, sorry i forgot the file stream in the parameters.
three, I accept never had whatsoever bug with it, although i would never utilise it where i was not sure whether the len of cord would go out of bounds.Sorry guys a quick idea when i was at work and posted in a bustle.
hoping to exist certified (programming in c)
hither'south the news - I'm officially certified.
05-28-2002 #11
Registered User
Okay folks compiled my lawmaking above and it crashed severely. It needed a few alterations. I donot know if information technology is more efficient than the other methods shown, simply it does work, as long every bit the length of the maximum line on the file is no longer than MAXSIZE.![]()
Lawmaking:
#include <stdio.h> #include <stdlib.h> #ascertain MAXSIZE 150 /*OR ANY LEN Yous Demand*/ int chief (void) { char str[150]; FILE *infp; int count = 0; if((infp = fopen("myfile.dat", "rt"))== Nix) { 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,"%[^\due north]\due north", str); count++; } printf("\n\nNumber of records on file is %d", count); getchar(); fclose(infp); return 0; }![]()
hoping to be certified (programming in c)
hither's 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, apply code tags: [code] /* insert code here */ [/lawmaking]
06-27-2002 #15
ATH0
Um... no. This is entirely wrong. What is this fifty-fifty supposed to do? First off, 'str' is an assortment, and you're dereferencing it ... and then you lot try to use .... yeah. Anyhow information technology's incorrect.while((*str = fgetc(infp))!= EOF) {
if( strch(str, '\n') )
count++;
}Quzah.
Hope is the kickoff pace on the road to disappointment.
Source: https://cboard.cprogramming.com/c-programming/18628-reading-data-file-till-there-newline.html
0 Response to "How to Read a File and Split It by Newlilne in C"
Postar um comentário