Thread: problem with file handling

  1. #1
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46

    problem with file handling

    Hi I have a txt file that I want my program to read. I use fstream. The file has the number 15 as the only thing in it. I want my program to read that 15 as an integer instead of a character string. Is that possible how do I convert a char string into an int. I know about typecasting but that doesn't work because it turns it into an integer that is huge and not the number 15. I think that is because it converts the string into the number of bits it took to store that string but I am not sure. So really my question is how do I get char thing[256]="15" to convert to int thingy = 15 ?
    Am I making sense? Thank you.

  2. #2
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    This is the only way I can think of, you'll have to write the code yourself.
    1. read the amount of digits in the string
    2. read the numbers one at a time and convert entire string into a int array.
    3. subtract 48 (value of zero as a char) from every number
    4. multiply the number by multiples of 10 and add them.

    "15\0" is the contents of you string
    1. 2 digits
    2. "49","54","\0"
    3. "1","5","\0"
    4. 1 (* 10(1)) + 5 (* 10(0))

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Look the documentation for sscanf - it should do exactly what you want.

    Eg. sscanf (mystring, "%d", &myint);

  4. #4
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    thanks i'll look into it

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    You could always use "atoi()" .... Array To Integer.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char thing[256];
    
    	sprintf(thing, "15");
    
    	int thingy = atoi(thing); //array to integer
    
    	cout << thingy << endl;
    
    	return 0;
    }

  6. #6
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Ah ha - ARRAY to integer - never could think of what the a was. HAHAHA .
    Do not make direct eye contact with me.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: problem with file handling

    Originally posted by gell10
    I think that is because it converts the string into the number of bits it took to store that string but I am not sure.
    No. If you have this:

    char * array = new char[256];

    or this:

    char array[256];

    then "array", by itself, is a pointer to the first element of the array. So array is a variable that stores the memory address of the first element of the array. When you cast to an int, you get the address of the array.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413

    Re: problem with file handling

    Originally posted by gell10
    I use fstream. The file has the number 15 as the only thing in it. I want my program to read that 15 as an integer instead of a character string.
    Come to think of it, isnt it simpler still to use:

    int num;
    in>>num; //assuming "in" is ifstream opened

    i.e. read it as an integer straight away, rather than using atoi(), which I thought stood for "alpha to integer" rather than "array to integer".

  9. #9
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    what if i wanted it to find a word in the text file and then read the number that immediately follows it? would i use sscanf?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  4. Problem while dealing with file handling functions
    By RoshanGautam in forum C Programming
    Replies: 3
    Last Post: 02-22-2006, 01:42 AM
  5. file handling and function problem.
    By prepare in forum C Programming
    Replies: 7
    Last Post: 10-09-2004, 02:26 AM