Get an int from a file

This is a discussion on Get an int from a file within the C++ Programming forums, part of the General Programming Boards category; Does anyone here know how to get an int from a file? What i mean is, take some numbers that ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    Get an int from a file

    Does anyone here know how to get an int from a file?
    What i mean is, take some numbers that are in a file, and store them in an int variable. when i try to go:

    int buffer;
    file.getline(buffer, 10);

    it gives me some error about cannot convert type blah blah

    and when i try to go:

    char buffer[11];
    file.getline((int)buffer, 10);

    it gives me the same error =[
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    file >> buffer;

    ?

    Or there is a series of ints? Or what?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    its a series of ints, like this:

    00|01|02 etc..
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    21
    If you have some input like
    00|01|02 then you cannot use it. Instead take the whole thing into an array and then separate the integers.
    [CODE]
    int main()
    {
    ifstream inFile;
    inFile.open("c:\\deneme.txt");
    char some[100];
    inFile>>some;
    //Take integers out of it
    //Do something
    return 0;
    }

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Ok is this anything to do with th www.cpp-home.com contest..



    Well in text text file everyting is stored as char...

    And buffer has to be of type char and not int.. It will not work if it is int.. What you can do is... Get the ASCII value of the char..

    char buffer;
    file.get(buffer);
    int ascii=buffer;



    so now in the ascii variable, value the ASCII value of the character is stored.. The ascii value for numbers 0,1 to 9 i think starts from 49, 50 etc etc.. So you can use an if statement like

    int array[100];

    if(ascii==49)
    array[1]=0;

    if(ascii==50)
    array[1]=1;



    and so on.. Now you get the numbers as integers in the array.. If you have any doubts about this.. Post a reply i will sort it out for you...

    Bye

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Is the file text? If yes and the integers are the same length, then use could:
    Code:
       char buffer[3];
       int value;
    
       in.getline(buffer,3);
       value = atoi(buffer);
       in.getline(buffer,3);
       value = atoi(buffer);
       in.getline(buffer,3);
       value = atoi(buffer);

  7. #7
    Unregistered
    Guest
    or use '|' as the delimiter for getline() instead of the default '\n'. That way you may have integers other than 2 digits that can still be read in successfully. If each line has 4 integers separated by a | and then the last integer is followed by a '\n', then use getline with '|' three times in a row and getline with a '\n' just once each loop through. If there are no '\n' in the file and it is all | separated then you don't need to do even that.

    If all you know is that the file is | separated but don't know how many integers per line, and don't know if there are new line char in the section of data being analyzed, then you need to read in char by char looking for any separaters.

    getline() combined with atoi() can be very flexible.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    yes, i am using | as the deliminator, i was just writing off of memory as i was not on the computer that has my files on it.

    what does atoi() do?

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >what does atoi() do?

    It converts strings to ints, but if you just want to read the first x number of ints from a file that are seperated by non-digit characters, you can use istreams inbuilt type checking, and do something like -

    Code:
    #include <iostream> 
    
    using namespace std; 
    
    int main() 
    {
    	const int count = 3;
    	int a[count];
    	int c=0;
    	
    	while (c<3)
    	{
    		cin >>a[c++];
    		if(cin.fail())
    		{
    			cin.clear();
    			cin.ignore();
    			--c;
    		}
    	}
    
    	cout << a[0] <<' '<<a[1]<<' ' << a[2];
    	
    	return 0;
    
    }
    This'll read the first 3 ints from cin (regardless of what is entered). Something similar will work for any number of ints or an ifstream in place of cin.

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    yea, but i dont want to use cin, i am trying to do file i/o
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  11. #11
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Then replace cin with your file i/o object. They are interchangable as both are are derived from the same base class.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    Code:
    char delete;
    
    while( delete != '\n' ){
        file >> myint;
        file.get( delete );
    }
    thisll get the number, eat the |, and loop until the new line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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