Reading a New Line From a File C++

#i

mackol is offline

Registered User


Reading data from a file till there is a newline?

this is what i am trying but information technology aint working

Code:

while (fptr == '\n')          counter++;
fptr is a file arrow and counter is counting the number of times there is a newline.

why is information technology wrong? and what would exist the correct lawmaking otherwise

tin can someone plz help
thanks


#two

Shadow is offline

Unleashed


The earth is waiting. I must leave you now.


#4

Hammer is offline

Terminate Of Line Hammer's Avatar


i recall this does the job

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.

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]


#seven

bigtamscot is offline

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.

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); }
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.

hoping to be certified (programming in c)
here'south the news - I'one thousand officially certified.


#8

Hammer is offline

End Of Line Hammer's Avatar


Originally 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.

Did y'all compile that, 'cos I couldn't. fscanf() is defined every bit

int fscanf( FILE *fp, const char *format, ... );

You are missing some parms. Also, fscanf render EOF, it doesn't identify it into i of its parameter variables.

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]


#9

Prelude is offline

Code Goddess Prelude's Avatar


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); }
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 /*any you think appropriate */  char fstr[MAX_LEN];  while ( fgets ( fstr, sizeof fstr, file ) != Nil )    counter++;
-Prelude

My best code is written with the delete fundamental.


#10

bigtamscot is offline

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.


#xi

bigtamscot is offline

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.


#12

Hammer is offline

End Of Line Hammer's Avatar


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]


#15

quzah is offline

ATH0 quzah's Avatar


while((*str = fgetc(infp))!= EOF) {
if( strch(str, '\n') )
count++;
}

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.

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel