Challenge - reading/writing file problem... Please HELP!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Challenge - reading/writing file problem... Please HELP!

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    1

    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

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center