Another File question

This is a discussion on Another File question within the C++ Programming forums, part of the General Programming Boards category; If this has been answered, forgive me. I have a series of files that I need to read in. I ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    51

    Another File question

    If this has been answered, forgive me.

    I have a series of files that I need to read in. I only care about the first field (seperated by spaces) on each line and each line is varying lengths.
    for example:
    Code:
    0200 this is the total number of widgets we need
    0030 this can be what ever
    Is there a way to use fscanf to read in the part I want and then move the file position pointer to the next line?

    Currently a fgets is being used and the data read is just trashed, it just seems there would be a better way than doing a disk read for no reason. Is there?

    current solution:
    Code:
    fscanf(file,"%x",buf);
    fgets(garbageBuffer,number,file);
    
    //next line
    fscanf(file,"%x",buf);
    fgets(garbageBuffer,number,file);
    
    // and so on
    thanks

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    No, those are C.
    Use getline with an std::string to read line-by-line. You can then convert the string using atoi which will convert the string until it encounters non-numbers and then stops.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    Excellent. Thank you for that.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can I just recommend that if we should be using a standard C [rather than C++ style stringstream, which indeed does seem a bit excessive], that instead of using "atoi", you use strtol(), which is a more competent (as in, it supports a bunch more features) and less error-prone function (as in, it accepts bad data gracefully and predictably, particularly when it comes to for example overflows - you get the highest/smallest number possible [INT_MAX/INT_MIN respectively], rather than "some random number that is anywhere between 0 and INT_MAX"

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

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Ah yes, atoi is C too, isn't it? >_<
    So the correct way would be to use the string streams or just go C with strtol.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Ah yes, atoi is C too, isn't it? >_<
    So the correct way would be to use the string streams or just go C with strtol.
    Yes, atoi() is an "before ANSI" type function, so it assumes most of the validation and checking is part of the programmers/users responsibility [although modern C-libraries MAY implement it as a call strtol() - for example GNU C library]. strtol is part of the ANSI standard library, so a bit more modern and robust.

    Remember, C was originally written for PDP-11 running Unix. PDP-11 has a 32KWord address space [64KB] for each application, so many of the early functions were rather rudimentary to not take up too much space.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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