Javascript required
Skip to content Skip to sidebar Skip to footer

How to Read a Newline in C

#1

mackol is offline

Registered User


Reading information from a file till in that location is a newline?

this is what i am trying but it aint working

Code:

while (fptr == '\due north')          counter++;
fptr is a file pointer 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 help
cheers


#2

Shadow is offline

Unleashed


The world is waiting. I must leave you now.


#4

Hammer is offline

Terminate Of Line Hammer's Avatar


i recall this does the chore

It does, but only for files that have lines <81 chars long. If you want to guarantee that you're counting all the newlines chars, you'd probably best check each character.

Hither'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 character will not be present when more northward-1 characters occur before the new-line. Also, a new-line character might non appear as the last character in a file, just before cease-of-file.

When all else fails, read the instructions.
If y'all're posting code, employ lawmaking tags: [code] /* insert lawmaking here */ [/code]


#7

bigtamscot is offline

Registered User


Another variation is to use the post-obit, skilful for counting the number of variable size records, each terminated by a new line character, on a text file.

Lawmaking:

#define MAX_LEN /*whatsoever yous think appropriate */  char str[MAX_LEN];  fscanf("%[^\n]\n", str); while(str != EOF)  {           counter++;           fscanf("%[^\due north]\n", str); }
the fscanf volition read the data upwardly to the new line character and the side by side "\n" takes reads the newline at the end of the record.

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


#8

Hammer is offline

Cease Of Line Hammer's Avatar


Originally posted past 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 upwardly to the new line grapheme and the adjacent "\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, ... );

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

Even when I fixed these $.25, information technology still didn't requite the right answer, so I'chiliad not sure the "%[^\north]\n" bit is correct.

When all else fails, read the instructions.
If yous're posting code, use code tags: [code] /* insert code hither */ [/code]


#9

Prelude is offline

Code Goddess Prelude's Avatar


Code:

#define MAX_LEN /*whatever you lot think appropriate */  char str[MAX_LEN];  fscanf("%[^\n]\northward", str); while(str != EOF)  {           counter++;           fscanf("%[^\n]\n", str); }
str is a reserved proper name, fscanf doesn't accept the correct arguments, str will never exist EOF, and the whole %[^\n]\northward format is just nasty. Even with the errors stock-still this lawmaking wouldn't give the right output, peradventure something more than like this:

Code:

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

My best code is written with the delete key.


#10

bigtamscot is offline

Registered User


1, no i never did compile it.
ii, deplorable i forgot the file stream in the parameters.
3, I have never had any problems with it, although i would never utilize information technology where i was not sure whether the len of string would get out of bounds.

Deplorable guys a quick idea when i was at work and posted in a hurry.

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


#eleven

bigtamscot is offline

Registered User


Okay folks compiled my code above and information technology crashed severely. Information technology needed a few alterations. I donot know if it is more efficient than the other methods shown, but information technology does work, every bit long as the length of the maximum line on the file is no longer than MAXSIZE.

Lawmaking:

#include <stdio.h> #include <stdlib.h>  #define MAXSIZE 150 /*OR ANY LEN YOU NEED*/  int main (void) {   char str[150];   FILE  *infp;   int count = 0;    if((infp = fopen("myfile.dat", "rt"))== Zippo) { 		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]\n", str); 			count++; 			}      printf("\n\nNumber of records on file is %d", count);   getchar();   fclose(infp);   return 0; }

hoping to exist 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 lawmaking....

When all else fails, read the instructions.
If you're posting code, employ code tags: [code] /* insert code here */ [/code]


#fifteen

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 array, and y'all're dereferencing information technology ... and then y'all endeavour to use .... yeah. Anyway information technology's wrong.

Quzah.

Hope is the first step on the road to disappointment.


How to Read a Newline in C

Source: https://cboard.cprogramming.com/c-programming/18628-reading-data-file-till-there-newline.html