Thread: Moving position in file to keywords

  1. #1
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45

    Moving position in file to keywords

    First I'll say hello since this is my first post.

    Ok, now I am experimenting with random file access and am curious as to how I should go about setting the current read/write position of the file to the space next to a keyword. For instance, if my file includes the line:

    EMPC 23

    I want to read in the integer 23. There has got to be a better way to do this than using fseek() manually.

    Also, when using fscanf(), and after some input is scanned, where does the file position go to? the next line?

    Thanks in advance for the help.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can simply use fscanf to read the string of characters in and also the integer value.

    Code:
    char buffer[80];  /* Or some other sufficient size */
    int value;
    FILE* fp;
    
    fp = fopen("whatever.txt","r");
    
    fscanf(fp,"%s %d", buffer, &value);
    No fseek required.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    Thanks for the reply, but maybe I wasn't clear.

    I want to know if there is a function to search down a file, and place the current read/write position after that keyword, or at the begininng of it at least.

    I could write a function to search down until a matching keyword is found, but I was hoping there was one already availible.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    There is no function like that in standard C.

    You can bypass fseek completely: call stat() to get a file size, then fread() to read the entire file into a buffer, then call one of the string search functions like strstr() to return the position of the search string in the buffer.

    This will be fine for smaller files, up to several MB or more -- depending on your hardware - memory mostly.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is this a text file? If so, the "integer 32" is not an integer at all. It's simply two bytes containing the characters '2' and '3'. Thus, you cannot simply change that to say, 1000, because that would be four bytes. There is no function in standard C that will allow you insert more, or remove some bytes in the middle of a file.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    That explains enough.

    All I want to do is read in the characters, from there I need to use the numbers, that is all. The file will be replaced by a newly generated one.

    Thanks for the help tho.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    FILE *fp = fopen("file.ext","r");
    char c;
    int i = 0;
    
    while(1) {
        c = fgetc(fp);
        if(feof(fp) || c=='\n') break;
    
        if(isdigit(c)) {
            i *= 10;
            i += (c-'0');
        }
    }
    
    printf("\nNumber from file: %i\n", i);
    Or of course you could just use fscanf().

    dwk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM