Please Tell me how to scan a source file in c..suppose a file i am giving as input and i have to scan that files for different things...can anybody help me!!!!!

6 Years
Discussion Span
Last Post by deceptikon

I mean to say if i am taking input a file and I have to scan it for numerous times an i have to take things from that file and i have to store in array and structure arrays......


Could you be any more vague? The thing is I made a bet with a coworker that "I have to scan a file for things" isn't the worst possible question one could ask if one is expecting any kind of useful answer.


what the hell are you talking,if you don't know then stay mum,what are you trying to prove here you moron!!!!!!


You ask a question that cannot be answered because there is no information given for us to understand and deceptikon is a moron? Check your mirror.

And you also make threads with urgent and plzz in the title in violation of the Member Rule "Do use clear and relevant titles for new articles". Neither title is clear nor relevant.

Then further violation of member rule "Do not post insults or personal attacks aimed at another member" just because you can't form a question properly.

Edited by WaltP


Hello mr .i just asked about the concept,if i had to post the code i would have done that...but i only wanted to know if any concept exists relevant to mu query if you are unable to understand you could have asked me gently also but you making fun of me....what you wrote was not personal attack it was quite nice,isn't it???When you will be needy some day you will understand my point...sorry for breaking your rule!!!!


ok boss i am basically trying to take a file as input in my program say "output" is the filename,now i did fopen("output","r").so now i want to scan this whole output file for someof its contents now when i find those contents i need to put in an struct array,coz it might be int,char any of data type....if you get me then ok else code is very large that's why i am not putting here...


It seems to me that the biggest issue here is the contents of the file being any possible type. C doesn't do dynamic typing very well, so at the very least you'll want to pare the problem down by selecting supported types and devising a file format that supports easy parsing. For example:

# Test file
I   1
S   this is a test
F   123.456

Then parsing the file is almost trivial:

#include <stdio.h>
#include <string.h>

#define DEBUG 1

typedef struct Record
    char type;
    char value[BUFSIZ];
} Record;

int main(void)
    FILE* in = fopen("test.txt", "r");

    if (in)
        char line[BUFSIZ], fmt[BUFSIZ];

        sprintf(fmt, "%%c %%%ds", BUFSIZ);

        while (fgets(line, sizeof line, in))
            char type, value[BUFSIZ];
            Record rec;

            if (line[0] == '#')

            if (sscanf(line, fmt, &type, value) == 2)
                rec.type = type;
                strcpy(rec.value, value);

            printf("Created record: {'%c', \"%s\"}\n", rec.type, rec.value);

            // Add the record to a collection for later use


    return 0;

The key here is that I'm storing the value as a string and taking note of its type. This way the value can later be converted to its corresponding type as necessary, without having to jump through hoops to store the value in its actual destination type.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.