Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a text file up to a certain character

Tags:

c

parsing

Here's my dilemma. I have a file, and wish to read in all characters up until the program hits a '#', and ignore everything on that line after the '#'. For example

0 4001232 0 #comment, discard

This is frustrating, as it feels like there is a very simple solution. Thanks!

like image 531
GG3 Avatar asked Mar 20 '11 00:03

GG3


People also ask

How do you read a character from file in C?

fgetc()– This function is used to read a single character from the file. fgets()– This function is used to read strings from files. fscanf()– This function is used to read formatted input from a file.


1 Answers

FILE *f = fopen("file.txt", "r");
int c;
while ((c = getc(f)) != '#' && c != EOF)
   putchar(c);
like image 61
Edward Karak Avatar answered Oct 05 '22 01:10

Edward Karak