File Reading Example Please?

This is a discussion on File Reading Example Please? within the C++ Programming forums, part of the General Programming Boards category; Basically i'v got a program that i need to update itself. Anytime you use it you enter various variables and ...

  1. #1
    #junkie
    Join Date
    Oct 2004
    Posts
    240

    File Reading Example Please?

    Basically i'v got a program that i need to update itself.

    Anytime you use it you enter various variables and it predicts something. Then i want you to input the variable you got from the outside source and it can update itself.
    (Since the outside source has a random factor, my program will be outputting the lowest, highest, medium, and possibly most common random medium)

    Well i can do that, my problem is File IO. Can someone give me an example of a working Dos program that reads a "format" persay? INI type would be great, but anything that is semi organised so i can look at it in a txt, and that is easily readable by a program, will do.

    I need it to hold multiple variable names with values, like ini's (item=value).

    Subcategories would be "nice", but really at this time since it is so none dynamic i could just write the names with categories (category1_name1=value, category2_name1=value)

    Big thanks to anyone who posts an example code.
    What specifically i would find helpful is a working code that reads a variable from a file, ie it searches and reads "mynameis=john" and prints the value "john", then asks you for your name and writes that to another variable in the same fashion. That should be plenty to get me going.


    Thanks a ton again if anyone can do this, thanks! (File IO has always .......... slapped me around for some reason lol)
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    First, some notes. You most likely aren't writing a DOS program. It's a windows program that runs in a console window. second, filenames mean nothing in the world of File IO. you could name the file in my examlpe 'Untitled1.exe' and it would still work perfectly fine. what you're looking for is called formatted data. the other type is binary data. formatted data can be read by a human and can be read in with the same functions you use with cin. binary data, on the other hand, has to be read in differently depending on the data type you wrote to the file in the first place. binary data files usually can't be read (or edited) directly by a human.

    with that cleared up, here's some example code:
    Code:
    #include<iostream>
    #include<fstream>   //for file IO
    
    int main()
    {
        char*info=new char&#091;20&#093;;
        std::fstream file("Untitled1.dat",std::ios::in);    //open the file for read
        
        for(register int i=0;i<3;i++)   //loop around 3 times - 3 pieces of data
        {
            file.ignore(32000,'='); //ignore up to and including the first '=' encountered
            file.getline(info,20,'\n'); //take in the rest of the line
            std::cout<<info<<std::endl;
        }
        
        file.close();   //explicitly close the stream
        std::cin.get();
        return 0;
    }
    and the file I used:
    Code:
    processor=x86
    OS=win2k
    Compiler=GCC
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    ya sorry, didnt mean dos.

    and i relized bout the file extension, just said that for ease. Though i was unaware of the comparision between binary & formatted data.

    Ok, so by what you gave me, and using the info from my books, i should be able to peice them together.

    Thanks!

    And other notes for searching/parsing a file format (or whatever you would like to call it, ie ini "format")
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  4. #4
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    couple questions also.

    1.) why are you using the class fstream? I cannot even find that class in my book(s) lol. All i'v seen them use is ofstream and ifstream for in and out. (not the header btw)

    3.) How can i control where the umm.. well i guess you could call it "curser" in the file is? In other words, when you ignore to the first '=', you're curse is now at that point. What if later on i want to go to the first one again?

    4.) if i use ofstream and ifstream, is there a problem with using them at the same time? or do they conflict if they have the file open at the same time. ie one reads, one edits, one reads and is all messed up because the file has changed when it was edited.



    I guess thats it, just wanted to know these things. Thanks!
    Last edited by Zeusbwr; 04-03-2005 at 01:50 PM.
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  5. #5
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    /bump

    And is it possible to search for strings? ie if i want to find say the 5th item named "blah5=" and later want to search for the 2nd item "blah2=", how would i do that? Ignore and getline only take in characters correct?


    Accordingly unless it is in a dif section (its been so long since i'v read it i cant remember it all lol), i cannot find any good.. or even partial ways to parse a file. Is there class functions that i should be aware of that are also used in ofstream/ifstream? Because i am having trouble switching between item names. Like if i want to retrieve a specific item name and write a new value to its value..
    Stuff like that..


    Any help would be awesome, thanks!
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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