1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to read only the integer value of a txt file in C++

Discussion in 'C++' started by Cabomba, Dec 18, 2007.

  1. Cabomba

    Cabomba New Member

    Joined:
    Dec 18, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I had a problem in reading a particular string of a text file in C++.
    For example, I had a text file that looks like the one below:

    Code:
    Variable = .iso.org.dod.internet.private.enterprises.9.9.273.1.3.1.1.3.1.8.105.118.116.108.95.97.112.49.0.2.45.4.155.81
    Value = Integer32 -44
    
    End of MIB subtree.
    I only wanted to read the integer "-44" from the text file in the C++ prog, but I donno how to read it without reading the "Variable = .iso.org.dod.internet.private.enterprises.9.9.273.1.3.1.1.3.1.8.105.118.116.108.95.97.112.49.0.2.45.4.155.81
    Value = Integer32 " strings.

    My partially written code to read the file is given as below:
    Code:
    apfile=fopen("ap1.txt","r+");
    
    if (apfile==NULL)
    {
    cout<<"Error opening file ap1.txt"<<endl;
    fclose (apfile);
    return -1;
    }
    
    else
    {
    while (!feof(apfile)) 
    {
    
    }
    }
    Anyone can help me to get only the integer value of "-44" from the ap1.txt in the C++ programming? Thanks alot for any help!
     
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    So do you want C or C++ ?

    Because you've used a random mix of C and C++ API's in your snippet of code, so I'm wondering what exactly you're trying to learn.

    > but I donno how to read it without reading the "Variable = .iso...
    You have to read it, but there's nothing to stop you from throwing it away and reading another line, the line with the info you want.
     
  3. Cabomba

    Cabomba New Member

    Joined:
    Dec 18, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Well, sorry for the confusion cos my programming aren't tht good and I've just started picking up C/C++ programming.

    My main purpose is to read the file, and to extract the integer "-44" value frm the text file, so I don really care if I'm using C or C++. But I'm still not sure of how to get rid of the first line and skipped to the second line, as the first line is pretty long and the length is random. anyway, thanks for ur time taken to reply to my question, greatly appreciate it! :)
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    I would suggest C++ then.

    Use a std::string variable to store each line, and use the getline() method to read a whole line (it's length won't matter).
     
  5. Cabomba

    Cabomba New Member

    Joined:
    Dec 18, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Can I try to open the text and put it in a 2 dimensional array, then call the second line of the text file in array format and frm there to use str.at() method to get the value "-44"? Can I do tht? Is it possible?
     
  6. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Sure you could do that.

    http://www.cplusplus.com/reference/string/string/
    But perhaps the find method (in a loop) to step over a number of spaces in the line would perhaps be more reliable.

    Why would you need a 2D array when you're only interested in the 2nd line?
    Just read the first line and throw it away, there's no need to store it.

    Try some ideas and see what happens :)
     

Share This Page