pointers searching through a file

This is a discussion on pointers searching through a file within the C Programming forums, part of the General Programming Boards category; hi guys say you have a extern file with numbers from 0-20. so it contains Code: 1\n2\n3\n4\n......\n20 the program's supposed ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    25

    pointers searching through a file

    hi guys

    say you have a extern file with numbers from 0-20. so it contains

    Code:
    1\n2\n3\n4\n......\n20
    the program's supposed to give all numbers greater than the number given on keyboard.

    my file's called List however. so the sketch would look like this:

    Code:
    FILE *pfile;
    
    pfile=fopen("C:\user\list.txt", "r+");
    
    
    if(pfile != NULL){
    
    .......  //my problem
    
    fclose(pfile);
    
    } 
     //it'd better follow with a else command in case the file doesn't work right
    now somehow I should run the pfile pointer through the data in the "list" file so it could find the one that are greater than the number given via scanf....

    and I can't figure how exactly...would running this work?

    Code:
    for(pfile=0;pfile<=20;pfile++){


    I'd be grateful for any hint....
    Last edited by Aitra; 01-19-2013 at 08:32 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    fcanf.

    Check the return value for EOF.

    Once you read a number, compare it with the number from keyboard. If it is ok, then print it. If not do nothing. Go the next number in the file ( a loop ).

    For the else part, have a message informing the user that the file could not open and exit with return -1;
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If pfile==NULL, there's no need to close fclose(pfile), because it was never opened. Print the file didn't open message\n and exit or return.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching through an Array using Pointers
    By towely in forum C Programming
    Replies: 18
    Last Post: 07-08-2011, 09:45 AM
  2. Searching through a file
    By sitestem in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2004, 09:00 AM
  3. Searching an array with pointers
    By pxleyes in forum C Programming
    Replies: 16
    Last Post: 03-23-2004, 04:07 PM
  4. Searching in file
    By Spedge in forum C Programming
    Replies: 2
    Last Post: 08-16-2003, 04:25 PM
  5. File Searching
    By phantobrain in forum C++ Programming
    Replies: 11
    Last Post: 06-15-2003, 07:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21