#include<stdio.h>

struct student{
	int id;
	char name[30];
	int mark;
};

void main()
{
FILE*fp;

struct student stud;
int id_no;
fp=fopen("data.txt","r);


printf("enter id no : ");
scanf("%d",&id_no);

while(fscanf(fp,"%d %s %d",stud.id,stud.name,stud.mark)!=EOF)
{

if(id_no == stud.id)
printf("%d %[^\n] %d\n",id_no,stud.name,stud.mark);

else

printf("Error !\n");

}
fclose(fp);

}

Hello Everyone,I am stuck at the middle how to print out the certain line from the the text file..for instance when the user enter id_no 12222 ,it should print out 12222 John Brown 80

Data.txt
22333 Smith Alison 70
12222 John Brown 80
12345 Chris Ratcel 60

anyone plz help :S :S

Alas, the %s format specificator reads only one word from the stream (for example, it reads from the 1st record Smith only but not Smith Allison. After that your fscanf wants to read integer but gets Allison and returns. The next fscanf starts from the current file position (on Allison word) and fails too...and so on...

Try another approach:

...
#define LSZ 256
    char line[LSZ];
    FILE* f;
    int id;
    f = fopen("data.txt","r");
    if (!f)
    {
        printf("Can\'t open data.txt\n");
        return 1;
    }
    do
        printf("enter id no : ");
    while (scanf("%d",&id) != 1); 

    if (f)
    {
        while (fgets(line,sizeof line,f) && atoi(line) != id)
            ;
        if (!feof(f))
            printf("%s",line);
        fclose(f);
    }
...

mancode1007, I recommend you to read a file byte by byte and store in a buffer and assume all data is character type and then do any process you wish.

I mean after reading your file your buffer will have a string like this

"22333 Smith Alison 70\r\n12222 John Brown 80\r\n12345 Chris Ratcel 60\r\n"

so you could create a loop and separate your data as you wish. if you need to change a numerical string to integer use atoi or similar functions.

if you read a file byte by byte ypu will have more flexiblity.

Since you're going to stop at each newline anyway, reading a whole line into a buffer using fgets() would be simpler than reading each char into a buffer.

Since you're going to stop at each newline anyway, reading a whole line into a buffer using fgets() would be simpler than reading each char into a buffer.

Yes, but if your file was not a text file, so you would face with some trouble ;)

Don't worry. The fgets stops at the proper time if the file is binary.

mancode,

look into the use of the "strstr" function.

Such a mass of bad suggestions! Arkm's 1st post is a good suggestion for the read, but alas, the suggestion for using feof() is fraught with peril, and his formatting is atrocious (please click the links). And his second post is just wrong.

Salem then reiterates the easiest and most flexible solution.

So look into reading the whole line and create a function that parses the line by looking for SPACEs and do something with the stuff between the SPACEs. That may be what the suggestion for strstr() is for, but since jephthah didn't say, it's not a very helpful suggestion.

my point is that STRSTR function will do what the OP wants

i thought it would be readily apparent to anyone who takes a moment to look it up. sorry, if i don't type out a fully commented and validated solution.

Essentially the solution is this:

get one line at a time with FGETS, then use STRSTR to see if a certain string (such as the particular "id_no") is found within the line. if not, then read the next line. if it is, well, there's your line.

i'll leave typing out the code as an exercise for the reader.

commented: I am stupendous man and I approve of this message. ;) +9

my point is that STRSTR function will do what the OP wants

i thought it would be readily apparent to anyone who takes a moment to look it up. sorry, if i don't type out a fully commented and validated solution.

Not necessarily. Without knowing what needs to be done, looking up a function generally is not of much help. After your explanation, your solution is readily apparent.

I'm not really criticizing your recommendation, just that how you meant to use it is a good thing to add.

commented: good point. +4

To WaltP The Judge:
It was funny to read your post about my suggestion.

About formatting - better read that link carefully:

The following are some suggestions and tips on formatting your C/C++ code. These are simply my preferences and in no way imply a standard.

About feof() in my snippet - it's absolutely safe and correct function call.

About my 2nd remark - it was absolutely correct note. The fgets can read binary (non-text) files safety. It never overwrite a text buffer and sets the last byte to zero.

Keep it simple, WaltP.

PS. No need to use strstr in this case, no need to parse input lines. A search key is the 1st field in a line so simple atoi can extract student id from the line (to print or not to print - that's a question).

About feof() in my snippet - it's absolutely safe and correct function call.

What if the last line contains the id being searched for and that line does not end with a newline?

We have ill-formed text file in that case.

Not necessarily. Without knowing what needs to be done, looking up a function generally is not of much help. After your explanation, your solution is readily apparent.

I'm not really criticizing your recommendation, just that how you meant to use it is a good thing to add.

what, you mean people cant read my mind?

:P

i do have a problem, sometimes, with being too terse. thanks for the reminder.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.