C++ file access

This is a discussion on C++ file access within the C++ Programming forums, part of the General Programming Boards category; I have a text file, either created manually, or as an output from a vb program (not yet completed, so ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    2

    Question C++ file access

    I have a text file, either created manually, or as an output from a vb program (not yet completed, so output format can be modified slightly), that I need to input into my c++ program. I need to be able to read a series of integers, characters and floats from the file, in order and assign these values to the correct type of variable.
    Currently, I can only open the file and read in the character values bit by bit or in blocks. Does anyone know how I can get this to work?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    ifstream will open a file for reading, as tho it were coming from cin

    ifstream input("filename.txt");
    int anInt;
    char aChar;
    double aDouble;
    float aFloat;

    if ( input.fail() )
    {
    cout << "Error unable to open filename.txt";
    return -1;
    }
    input >> anInt >> aChar >> aDouble >> aFloat;

    That would read an int, a char, a double, then a float from the file "filename.txt"

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, 11:39 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM

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