Read Until End of Line C Read File

#1

mackol is offline

Registered User


Reading information from a file till at that place is a newline?

this is what i am trying just it 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 it wrong? and what would be the right code otherwise

tin someone plz assistance
cheers


#2

Shadow is offline

Unleashed


The world is waiting. I must leave you now.


#iv

Hammer is offline

End Of Line Hammer's Avatar


i recall this does the task

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.

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]


#7

bigtamscot is offline

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.

Lawmaking:

#define MAX_LEN /*whatever you think appropriate */  char str[MAX_LEN];  fscanf("%[^\n]\n", str); while(str != EOF)  {           counter++;           fscanf("%[^\n]\n", str); }
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.

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


#viii

Hammer is offline

End Of Line Hammer's Avatar


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

Did you compile that, 'cos I couldn't. fscanf() is defined as

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

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

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]


#9

Prelude is offline

Code Goddess Prelude's Avatar


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

My best lawmaking is written with the delete primal.


#10

bigtamscot is offline

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.


#11

bigtamscot is offline

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.


#12

Hammer is offline

End Of Line Hammer's Avatar


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]


#xv

quzah is offline

ATH0 quzah's Avatar


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

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.

Quzah.

Hope is the first step on the road to disappointment.


milesaropporry.blogspot.com

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel