Thread: Print Certain Line in C

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    5

    Print Certain Line in C

    Code:
    #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 ...thx

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What result are you getting, and how is that different from what you expect?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    &#37;s in scanf reads till the first white space encountered so stud.name will be
    "John" not "John Brown"

    use return value of scanf to see how many formats were parsed successfully

    with the file structure presented you need to read firstName and lastName into 2 different buffers
    you cannot read them together

    to avoid problems after failed parsing - better use pair fgets/sscanf instead of fscanf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    1
    Hello there!

    I'm having difficulties with understanding what's happening with scanf() in combination with character formatter -&#37;c.

    Let's look at simple example:

    Code:
    #include <stdio.h>
    int main(void)
    {
      
      char c;
    
      scanf("%c",&c);
      scanf("%c",&c);
    
      system("PAUSE");	
      return 0;
    }
    Firstly printf() parses all newly typed data, so if I press character 'p' and then enter, the data would be: 'p','\n'.Since I provided character formatter, I'd expect that first character is taken and all additional are removed, but it seems they aren't.Now the interesting part; the 'p' seems to get assigned to variable c and new line-'\n' character is sent to input and waits to be parsed and that happens in second scanf() call.

    So what's the problem?Some guys told me that I have to supply a '\n' char in first argument.

    Code:
     scanf("%c\n",&c);
    Thing worked, but the question is why it worked?From here I can see only a new character added, which should create a new input information(one more loop), but no.Looks like this '\n' character in this context serves as "ignore \n data coming from input" or smth?

    And yes, I could use getstr(), but I'm kind of "Always the hard way" guy...

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by W1ck3D View Post
    Hello there!

    I'm having difficulties with understanding what's happening with scanf() in combination with character formatter -%c.

    Let's look at simple example:

    Code:
    #include <stdio.h>
    int main(void)
    {
      
      char c;
    
      scanf("%c",&c);
      scanf("%c",&c);
    
      system("PAUSE");	
      return 0;
    }
    Firstly printf() parses all newly typed data, so if I press character 'p' and then enter, the data would be: 'p','\n'.Since I provided character formatter, I'd expect that first character is taken and all additional are removed, but it seems they aren't.Now the interesting part; the 'p' seems to get assigned to variable c and new line-'\n' character is sent to input and waits to be parsed and that happens in second scanf() call.

    So what's the problem?Some guys told me that I have to supply a '\n' char in first argument.

    Code:
     scanf("%c\n",&c);
    Thing worked, but the question is why it worked?From here I can see only a new character added, which should create a new input information(one more loop), but no.Looks like this '\n' character in this context serves as "ignore \n data coming from input" or smth?

    And yes, I could use getstr(), but I'm kind of "Always the hard way" guy...
    You have dug and figured out exactly how scanf() works. Good job. And yes, that's that way it works, just like the Sun rises in the East and sets in the West.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    All you need is to create a function that has 3 arguments: pointer to file, string to search, size of string then you should search for the string in your text file (if you ask "how?" the answer will be "think").
    goodluck
    gavra.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Sometimes I wonder about you, Salem. You are like the the world-wide-stalker (and I mean it in the most endearing way possible).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. if is faster than switch?
    By skorman00 in forum C++ Programming
    Replies: 32
    Last Post: 03-06-2004, 01:15 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM
  5. fprintf help ,my print outs wont line up
    By plivermo in forum C Programming
    Replies: 2
    Last Post: 08-15-2001, 09:39 AM