开发者

Trying to read text file C++

开发者 https://www.devze.com 2023-02-23 20:50 出处:网络
Using Dev C++, we are trying to get the code to read it one line at a time and store it in an array. We don\'t seem to get any official errors, but a windows box pops onto the screen to find a solutio

Using Dev C++, we are trying to get the code to read it one line at a time and store it in an array. We don't seem to get any official errors, but a windows box pops onto the screen to find a solution for the errors?

#include <iostream>
#include <fstream>
#include "AddressBook.h"

const int ADDR_BOOK_SZ  = 1000;

void AddNewAddressBook(AddressBook* current);
void PrintAddresses(AddressBook* addrBook);

using namespace std;
int main(int argc, char** argv) {
    AddressBook addrBook[ADDR_BOOK_SZ];
    AddressBook* current;
    char* path;
    ifstream file;
    char* placeholder;
    bool running = true;
    char entered;
    while(running) {
        //print directions
        cout << "a) Open an address book file\n" 
        << "b) Add a new address book entry\n" 
        << "c) Print the contents of current address book\n" 
        << "d) Quit" << endl;

    //get the user's command
    cin >> entered;

    //set pointer to the current addressbook
    current = addrBook + AddressBook::entryCnt_;

    if(entered == 'a') {
         cout << "Please enter the file path for the address book file: " << '\n';
          cin >> path;
          file.open(path);
          int i = 0;
          while(!file.eof()){

              //getline(placeholder, 100);
              file >> placeholder;
              addrBook[i].SetFirstName(placeholder);
              file >> placeholder;
              addrBook[i].SetFirstName(placeholder);
              file >> placeholder;
              addrBook[i].SetStreetNum((int)placeholder);
              file >> placeholder;
              addrBook[i].SetStreetName(placeholder);
              file >> placeholder;
              addrBook[i].SetCity(placeholder);
              file >> placeholder;
              addrBook[i].SetState(placeholder);
              file >> placeholder;
              addrBook[i].SetZipCode((int)placeholder);            
开发者_JS百科              i++;      
          }
    } 
    else if(entered == 'b') {
        current->AddEntryFromConsole();

    }
    else if(entered == 'c') {
        for(int i = 0; i < AddressBook::entryCnt_; i++) {
            addrBook[i].PrintToConsole();
        }
    }
    else if(entered == 'd') {
        return 0;
    }
    else {
        cout << "Wrong input entered. Try again." << endl;
    }
}
file.close();

}

Thank you for any and all help!


What does ifstream::operator>> do to an unitialized char* (or an initiazized char* for that matter). I would guess you want a string instead of a char*==right now I think you're reading into random memory.

So, to be clear, try replacing

char* path;

with

string path

and

char* placeholder;

with

string placeholder;

You'll also need to add:

#include <string>

Mind you, this is just based on a quick code review.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号