[C] Skipping comments in a text file

Started by
16 comments, last by yewbie 15 years, 8 months ago
Quote:Original post by Gage64
What if the numbers can be more then one digit and I want to use fscanf() to read them and not do the parsing myself?
*shrug* fscanf doesn't support that. That has no bearing on your particular problem, though, so I'm not sure why you ask.
Advertisement
Quote:Original post by Sneftel
Quote:Original post by Gage64
What if the numbers can be more then one digit and I want to use fscanf() to read them and not do the parsing myself?
*shrug* fscanf doesn't support that. That has no bearing on your particular problem, though, so I'm not sure why you ask.


In your example you're right, but for my example it will work (I assume there's some space between a number and the # following it).

skipComments(file);fscanf(file, "%d", &num1);skipComments(file);fscanf(file, "%d", &num2);skipComments(file);fscanf(file, "%d", &num3);


Of course, like you said, this will be cleaner using a readInt() function, but that function will also need to use skipComments().
Read the line. Strip any trailing comment. If the result is empty, move to the next line. Otherwise, use sscanf to read the number. Seriously, I don't mean to be snide, but how are you not getting this?
Quote:Original post by Sneftel
how are you not getting this?


My thoughts exactly...

Quote:Read the line. Strip any trailing comment. If the result is empty, move to the next line. Otherwise, use sscanf to read the number.


I thought as much, but I don't know if it's any better than the snippet I posted above, given that more than one number can appear on a line.

Anyway, thank you and everyone else for their help.
Can there be anything in between numbers on a line?

Can there be anything before numbers on a line?

Is a particular number of numbers expected on any given line? Is there a limit?

Does the program care how many numbers are on each line? Or is it just trying to grab all the numbers in the file and put them in a single sequence?
Quote:Original post by Zahlman
Can there be anything in between numbers on a line?

Can there be anything before numbers on a line?


Only white-space.

Quote:Is a particular number of numbers expected on any given line? Is there a limit?

Does the program care how many numbers are on each line? Or is it just trying to grab all the numbers in the file and put them in a single sequence?


There are only 4 "tokens". The first is a magic number consisting of two characters ("P3" or "P6"), then 3 more numbers - width, height and depth. Each token is not necessarily on a separate line.

A comment can appear anywhere before the depth (I'm guessing it can't appear before the magic number, but this isn't specified so I'm not assuming it).

Also, I assume that there's some space after a token and before a trailing '#'. This isn't specified either, but if it's not true than (I think) I can't use fscanf() anyway, and wanting to use it is basically the reason I started this thread. So let's assume it's true just for the sake of discussion.
#include <stdio.h>#include <ctype.h>int skip(FILE *fp){	int c=fgetc(fp); while(isspace(c)) c=fgetc(fp);		if(c=='#')		{		while(c!=EOF && c!='\n') c=fgetc(fp);		return skip(fp); // honestly, this will work :)		}			return c;}enum { eof,number,error };int next(FILE *fp,char *token){    int c=skip(fp);    if(c==EOF) return eof;        if(isdigit(c)) // or isalnum() or whatever		{		while(isdigit(c)){ *token++=c; c=fgetc(fp); }		ungetc(c,fp);				*token='\0';				return number;		}	return error;}int main(){	char token[256];	FILE *fp=fopen("test.txt","r");		if(fp)		{		int n=next(fp,token);		while(n!=eof)			{			if(n==error){ printf("syntax error\n"); fclose(fp); return -1; }			printf("[%s]\n",token);			n=next(fp,token);			}		fclose(fp);		}}


Obviously you want to add checking for buffer overflow when building the token.
I used something like this, and if it does not = // just else it.

fptr1 = fopen ("data/accounts.txt" , "r");	if (fptr1 == NULL) //if we failed to open the file for some reason		{				cout << "**Failed To Load Accounts.txt File**\n";			ErrorToFile("**Failed To Load Accounts.txt File**");//send the error to our file			return;		}	else 		{			char line[MAX_STRING];			while(fgets(line, MAX_STRING, fptr1)) 				{					if(line[0] == '/' && line[1] == '/')  //we are skipping this line						{ // skip comment line							continue;						}                                }                }

This topic is closed to new replies.

Advertisement