Reading a New Line From a File C++
05-28-2002 #i
Registered User
Reading data from a file till there is a newline?
this is what i am trying but 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 information technology wrong? and what would exist the correct lawmaking otherwise
![]()
tin can someone plz help
![]()
thanks
05-28-2002 #two
Unleashed
The earth is waiting. I must leave you now.
05-28-2002 #4
Terminate Of Line
It does, but simply for files that have lines <81 chars long. If yous want to guarantee that you're counting all the newlines chars, y'all'd probably best check each character.i recall this does the job
Here's an extract from my manual for fgets():
A common programming fault is to presume the presence of a new-line grapheme in every string that is read into the assortment. A new-line character will not be present when more than n-1 characters occur before the new-line. Too, a new-line graphic symbol might non announced as the concluding character in a file, simply before cease-of-file.
When all else fails, read the instructions.
If you're posting code, employ code tags: [code] /* insert lawmaking here */ [/lawmaking]
05-28-2002 #seven
Registered User
Another variation is to use the following, good 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 up to the new line character and the next "\n" takes reads the newline at the finish of the record.Code:
#ascertain MAX_LEN /*any you remember appropriate */ char str[MAX_LEN]; fscanf("%[^\n]\n", str); while(str != EOF) { counter++; fscanf("%[^\due north]\northward", str); }
hoping to be certified (programming in c)
here'south the news - I'one thousand officially certified.
05-28-2002 #8
End Of Line
Did y'all compile that, 'cos I couldn't. fscanf() is defined every bitOriginally posted by bigtamscot
Another variation is to utilise the following, adept for counting the number of variable size records, each terminated by a new line character, on a text file.
<-- snip code -->
the fscanf will read the data up to the new line grapheme and the next "\n" takes reads the newline at the end of the record.
You are missing some parms. Also, fscanf render EOF, it doesn't identify it into i of its parameter variables.int fscanf( FILE *fp, const char *format, ... );
Fifty-fifty when I fixed these $.25, it however didn't give the correct answer, so I'thousand non certain the "%[^\n]\northward" bit is correct.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/lawmaking]
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 %[^\northward]\north format is just nasty. Even with the errors fixed this code wouldn't give the correct output, perhaps something more than like this:Code:
#define MAX_LEN /*whatever you call back advisable */ char str[MAX_LEN]; fscanf("%[^\n]\northward", str); while(str != EOF) { counter++; fscanf("%[^\due north]\n", str); }
-PreludeCode:
#define MAX_LEN /*any you think appropriate */ char fstr[MAX_LEN]; while ( fgets ( fstr, sizeof fstr, file ) != Nil ) counter++;
My best code is written with the delete fundamental.
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 have never had any problems with it, although i would never use it where i was not sure whether the len of string would go out of bounds.Sad guys a quick thought when i was at work and posted in a hurry.
hoping to be certified (programming in c)
hither's the news - I'm officially certified.
05-28-2002 #xi
Registered User
Okay folks compiled my code above and it crashed severely. Information technology needed a few alterations. I donot know if it is more efficient than the other methods shown, but it does work, as long equally the length of the maximum line on the file is no longer than MAXSIZE.![]()
Code:
#include <stdio.h> #include <stdlib.h> #ascertain MAXSIZE 150 /*OR Whatsoever LEN YOU NEED*/ int main (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 (1); } printf("\nNow counting number of records on file"); while((*str = fgetc(infp))!= EOF) { fscanf(infp,"%[^\n]\north", str); count++; } printf("\north\nNumber of records on file is %d", count); getchar(); fclose(infp); render 0; }![]()
hoping to be certified (programming in c)
here's the news - I'thou officially certified.
05-28-2002 #12
End Of Line
Accept another read of Preludes comments on your code....![]()
When all else fails, read the instructions.
If you're posting code, utilise lawmaking tags: [code] /* insert code hither */ [/code]
06-27-2002 #15
ATH0
Um... no. This is entirely incorrect. What is this even supposed to practise? First off, 'str' is an assortment, and you're dereferencing it ... and and then you attempt to use .... yeah. Anyway information technology'due south wrong.while((*str = fgetc(infp))!= EOF) {
if( strch(str, '\n') )
count++;
}Quzah.
Hope is the beginning step on the road to thwarting.
friendtherinceple.blogspot.com
Source: https://cboard.cprogramming.com/c-programming/18628-reading-data-file-till-there-newline.html
0 Response to "Reading a New Line From a File C++"
Post a Comment