Reading from a text file.

This is a discussion on Reading from a text file. within the C Programming forums, part of the General Programming Boards category; i want to assign what is read from a text file to an int. In the text file there will ...

  1. #1
    Unregistered
    Guest

    Reading from a text file.

    i want to assign what is read from a text file to an int. In the text file there will only be 1 number such as 1000 or 3023. I want to do this to have a High Score system in my black jack game, any ideas?


    here is the code i am using to read from a file


    fp=fopen("high scores","r");

    if ( fp != NULL )
    {
    while ( (highscore = fgetc( fp )) != EOF )
    putchar( highscore );

    test=highscore;


    fclose( fp );
    }

    i would like to assign the number that is read to an int.


    -thanks

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    the problem with reading from files is that it stores it as a char or string
    not an int`
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    fgetc() only gets one character from a file, so really doesn't do what you want directly. The easiest way would be to would fgets() to read the data into a string, the use atoi() or atol() to convert it to a number.

    Here's some sample code that might help you
    Code:
    if (fgets(buffer, sizeof buffer, fp) != NULL)
    	printf("%d\n", atoi(buffer));
    else
    	printf("Error: Number not found\n");
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Unregistered
    Guest

    if i use that.

    if i use the code above, how would i right to a file?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    fprintf(fp, "%d", i);

    I presume you are talking about storing your highest score?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 02:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM

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