CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old October 23rd, 2009, 12:09 AM
    kamil607 kamil607 is offline
    Junior Member
     
    Join Date: Oct 2009
    Posts: 1
    kamil607 is an unknown quantity at this point (<10)
    Angry Challenge - reading/writing file problem... Please HELP!

    I am new here and I have a problem with a piece of code. I tried may ways to get this code to work but still nothing.

    The code consists of 3 major parts:
    menu(void);
    writeData(void);
    readData(void);
    functions.

    I was able to create

    menu(void);
    writeData(void);

    but I cannot do :
    readData(void);

    I need to use the comments to make it work the way it should.



    The output should be like this:

    Show records:
    __________________
    Record #1
    Name.... Smith Lol
    Street...... street something
    City...... Any Town
    State..... TX
    Zip 83949
    __________________
    Record #2
    Name.... Smith Lol
    Street...... street something
    City...... Any Town
    State..... TX
    Zip 83949
    __________________
    .
    .
    .



    HERE IS THE CODE I GOT:

    //Specification: Append and display records in a address database
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    void menu(void);
    void writeData(void);
    void readData(void);
    string * split(string, char);

    const char FileName[] = "File.txt";
    ofstream writingFile ("File.txt", ios:ut);
    ifstream readingFile ("File.txt");
    int main () {
    menu();
    return 0;
    } //end main


    void menu(void) {
    //allow user to choose to append records, display records or exit the program
    cout << "(E)xit , Choose to (A)ppend, (D)isplay records"<< endl <<endl;
    char choice =' ';
    cin>> choice;
    switch (choice)
    {
    case 'E':
    case 'e':

    break;

    case 'A':
    case 'a':
    writeData();
    break;

    case 'D':
    case 'd':
    readData();
    break;

    default:
    cout<< "ERROR";
    break;
    }

    }//end menu
    void writeData(void){
    //Write the Address Info to a file
    char endEntry;
    if (writingFile.is_open()){
    do{
    string name;
    string street;
    string city;
    string state;
    string zip;
    cout << "Name..... ";
    getline(cin, name);
    getline(cin, name); // I need to put that here (more info about this problem can be found here: http://en.allexperts.com/q/C-1040/Getline.htm)
    cout << "Street..... ";
    getline(cin,street);
    cout << "City..... ";
    getline(cin,city);
    cout << "State.... ";
    getline(cin,state);
    cout << "Zip..... ";
    getline(cin,zip);

    writingFile << name << "," << street << "," << city<< ","<< state <<"," <<zip << endl;
    cout << "Enter another Record? (Y/N)";
    cin >> endEntry ;
    }while ((endEntry == 'Y') || (endEntry == 'y'));

    menu();
    }
    writingFile.close();
    }

    //end write data


    void readData(void){
    string stringLine;
    if (readingFile.is_open()){
    getline(readingFile, stringLine);
    while(!readingFile.eof()){
    char theDeliminator = ',';
    split(stringLine, theDeliminator);
    }
    }
    else cout << "ERROR";
    readingFile.close();



    //read data from a file
    //use the split function to break a
    //deliminated line of text into fields
    }//end read data


    string * split(string theLine, char theDeliminator){
    //Break theline into fields and save the fields to an array.
    //Each field will occupy one element in a character array.
    //theLine is a string with fields separated with theDeliminator character.
    //Assumes the last field in the string is terminated with a newline.
    //Useage: string *theFields = split(lineBuffer, ',');

    //determine how many splits there will be so we can size our array
    int splitCount = 0;
    for(int i = 0; i < theLine.size(); i++){
    if (theLine[i] == theDeliminator)
    splitCount++;
    }
    splitCount++; //add one more to the count because there is not an ending comma
    //create an array to hold the fields
    string* theFieldArray;
    theFieldArray = new string[splitCount];
    //split the string into seperate fields
    string theField = "";
    int commaCount = 0;

    for(int i = 0; i < theLine.size(); i++){ //read each character and look for the deliminator
    if (theLine[i] != theDeliminator) {
    theField += theLine[i]; //build the field
    }
    else { //the deliminator was hit so save to the field to the array
    theFieldArray[commaCount] = theField; //save the field to the array
    theField = "";
    commaCount++;
    }
    }
    theFieldArray[commaCount] = theField; //the last field is not marked with a comma...

    return theFieldArray;
    } //end split
    Reply With Quote
      #2    
    Old October 23rd, 2009, 03:32 AM
    olivthill2 olivthill2 is offline
    Member
     
    Join Date: Apr 2009
    Posts: 291
    olivthill2 is a jewel in the rough (200+) olivthill2 is a jewel in the rough (200+) olivthill2 is a jewel in the rough (200+)
    Re: Challenge - reading/writing file problem... Please HELP!

    1. Please give a good description of the problem.
    Just saying "still nothing" is not enough, and besides it is not true.

    2. Please, use tags when you copy your program here.
    Write [code] before your first line of code, and [/code] after your last line. This way your program will be much more easy to read, e.g.
    Code:
    //Specification: Append and display records in a address database 
    #include <iostream>
    ....
    3. Please write "Urgent" in the title.
    Reply With Quote
    Reply

    Bookmarks

    Tags
    files , functions , reading , writing
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:15 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.