Reading a text file up to a certain character

2019-06-20 00:57发布

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!

标签: c parsing
5条回答
啃猪蹄的小仙女
2楼-- · 2019-06-20 01:23

There are plenty of ways and examples of how to do it. Usually, the idea is to have a variable that holds the state (before #, after # , after \n etc.) and run in a while loop until EOF. an example you can see here it's a program to remove C comments, but the idea is the same.

查看更多
一纸荒年 Trace。
3楼-- · 2019-06-20 01:29
FILE *f = fopen("file.txt", "r");
int c;
while ((c = getc(f)) != '#' && c != EOF)
   putchar(c);
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-06-20 01:30

Read a line using fgets, read through this line till you get a '#' character.

Read an another line...

查看更多
Rolldiameter
5楼-- · 2019-06-20 01:41

This is a bit more of a pre-processing than parsing question in my mind. Anyway there are number of tool and commands that specialize in doing just what you ask. If possible, it is probably best to use them.

If however you need or want to do so inside your code than the general method for doing so is, as was already mentioned, to keep the current state your in and handle any new character according to the state. This is a very good general method and is highly recommended, especially is there is more pre-processing that needs to be done.

If however this is the absolutely the only thing you what to do, than you can do something a little bit better and forgo the state with a code like this:

do {
   // Initialize things (buffer for the characters maybe) per line
   ch = fgetc(input_file);
   while ( (ch != EOF) && (ch != '\n') && (ch != '#') ) // Assuming # is the comment character
   {
        // Do something with 'ch', save it to a buffer, give it to a function - whatever
        ch = fgetc(input_file);
   }
   // If you save the characters to a buffer, this will be a good time to do something with it
   while ( (ch != EOF) && (ch != '\n') ) ch = fgetc(input_file); // Read the rest of the line
while ( ch != EOF );
查看更多
Rolldiameter
6楼-- · 2019-06-20 01:46

The solution depends on how you are "reading" that.

I could, for example, just remove all of those comments with sed 's/#.*//' <infile >outfile in bash.

EDIT: However, if I was parsing it manually, I could simply (in my loop for parsing it) have

if(line[i]=='#') {
    continue;
}

which would stop parsing that line by exiting the loop.

查看更多
登录 后发表回答