How to Read a File and Split It by Newlilne in C

#1

mackol is offline

Registered User


Reading data from a file till there is a newline?

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

can someone plz help
thanks


#2

Shadow is offline

Unleashed


The world is waiting. I must leave you now.


#four

Hammer is offline

End Of Line Hammer's Avatar


i think this does the chore

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.

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]


#seven

bigtamscot is offline

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.

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

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


#8

Hammer is offline

Terminate Of Line Hammer's Avatar


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

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

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

Yous are missing some parms. Besides, fscanf return EOF, it doesn't place it into ane of its parameter variables.

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]


#9

Prelude is offline

Code Goddess Prelude's Avatar


Code:

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

#define MAX_LEN /*any you lot retrieve appropriate */  char fstr[MAX_LEN];  while ( fgets ( fstr, sizeof fstr, file ) != NULL )    counter++;
-Prelude

My best lawmaking is written with the delete cardinal.


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


#11

bigtamscot is offline

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.


#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, apply code tags: [code] /* insert code here */ [/lawmaking]


#15

quzah is offline

ATH0 quzah's Avatar


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

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.

Quzah.

Hope is the kickoff pace on the road to disappointment.


leesonot1954.blogspot.com

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel