954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Print Certain line in C

#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

mancode1007
Newbie Poster
13 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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);
    }
...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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.

VatooVatoo
Light Poster
44 posts since Jan 2007
Reputation Points: 20
Solved Threads: 2
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
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 ;)

VatooVatoo
Light Poster
44 posts since Jan 2007
Reputation Points: 20
Solved Threads: 2
 

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

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

mancode,

look into the use of the "strstr" function.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

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 sincejephthah didn't say, it's not a very helpful suggestion.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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).

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
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?

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

We have ill-formed text file in that case.

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You