silly file I/O problems.

This is a discussion on silly file I/O problems. within the C++ Programming forums, part of the General Programming Boards category; In a program I'm writing, I'm trying to utilize the data of a file in a way where parts of ...

  1. #1
    n3v
    n3v is offline
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67

    silly file I/O problems.

    In a program I'm writing, I'm trying to utilize the data of a file in a way where parts of it will be interpreted into variables. The only way I could think of doing this would be to be to put the contents of the file into a vector, and find the variables based on their position in the file. is this a good way? regardless, I've run into some problems performing my method, simply because i seem to suck at file I/o and also using vectors. My code on putting the file into a vector Seems to work, but I tried to test it by writing a few lines of code that would output the contents of the vector into a text file, which should be identical to the test file. Anyway, my code for this segment of my program is as follows:
    Code:
    '
        char buff;
        vector <char> arch;
        ifstream model;
        ifstream testoutput;
        vector <int>::iterator n;
        
        model.open("C:\\test.x");
        while (model.get(buff)) {
        arch.push_back(buff);
        }   
        model.close();
        testoutput.open("C:\\output.txt", ios::trunc);
        
        for(n = arch.begin();n != arch.end(); n++){
        testoutput << *n;
        }
        testoutput.close();
    If you make a man a fire,
    he will be warm for a day.
    If you set a man on fire,
    he will be warm for the rest of his life.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    You write to ofstream objects and read from ifstream objects. ALso, your iterator needs to be char not int.
    Code:
    char buff;
    vector <char> arch;
    ifstream model;
    ofstream testoutput;
    vector <char>::iterator n;
        
    model.open("C:\\test.x");
    while (model.get(buff)) {
        arch.push_back(buff);
    }   
    model.close();
    testoutput.open("C:\\output.txt", ios::trunc);
        
    for(n = arch.begin();n != arch.end(); n++){
       testoutput << *n;
    }
    testoutput.close();


    How I'd do this:
    Code:
    vector <char> arch;
    {
        ifstream model("C:\\test.x");
        copy(istream_iterator<char>(model),istream_iterator<char>(),back_inserter(arch));
    }   
    
    {
        ofstream testoutput("C:\\output.txt");
        copy(arch.begin(),arch.end(),ostream_iterator<char>(testoutput));
    }
    Last edited by hk_mp5kpdw; 06-18-2007 at 05:50 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    n3v
    n3v is offline
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67
    thanks a lot, i figured my problems were a lot worse than just that to be honest, haha.

    I'm running into another snag now though. my file contains a number with about twelve numbers behind the decimal point, and i need to put that all into one float , how would i go about extracting that multitude of char values from the vector and converting them to a single float? I'm totally lost when it comes to this.

    Edit:
    also, i opted not to use your second bit of code, hk_mp5k, simply cause i didn't really understand it. I don't get the vector iterator functions you used there, nor have i seen the back_inserter() function. any information would be greatly appreciated. thanks.
    Last edited by n3v; 06-19-2007 at 05:57 AM.
    If you make a man a fire,
    he will be warm for a day.
    If you set a man on fire,
    he will be warm for the rest of his life.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Could you post a sample file? If your program also writes the file, then may-be you can choose a better format for it.

    Otherwise you might try using strings and stringstreams.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    n3v
    n3v is offline
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67
    i'm making a program that displays directx model files in the .x format. a sample of the file looks like this:
    Code:
    xof 0302txt 0064
    
    Header{
    1;0;1;
    }
    Mesh Mesh_Cube {
    24;
    1.0; 0.999999940395; -1.0;,
    1.0; -1.0; -1.0;,
    -1.00000011921; -0.999999821186; -1.0;,
    -0.999999642372; 1.00000035763; -1.0;,
    1.00000047684; 0.999999463558; 1.0;,
    If you make a man a fire,
    he will be warm for a day.
    If you set a man on fire,
    he will be warm for the rest of his life.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM

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