1

I'm creating a simple directory program that allow user to enter a file name and search that file by name, address, or phone number. I'm having trouble properly reading the file.

If someone could give me suggestions on how to fix my getFirstName function. The function should read the first word of the file.

Example file:

Bob Smith 123456789
123 Main Street
Susan Smith 1224445555
543 Market Street

Here is part of my code so far.

 string file;
 string first;
 int main() {
     ifstream inFile;
     cout<<"Enter file name: ";
     cin>> file;
     inFile.open(file);
     if(inData.fail()) {
         cout<<"INVAILD";
     }
     getFirstName(first);
}

void getFirstName(string f, inFile file) {

    file.open(f);
    while(file.good(f)) {
        file>>f;
    }
    if (file.bad()) {
        cout<<"Name not found";
    }
}
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
Taylor
  • 23
  • 5
  • 7
    You have loads of syntactical problems in your code. Go through the [compiler error messages](http://coliru.stacked-crooked.com/a/32a9944e6d5d51c2) one by one and fix them. – πάντα ῥεῖ Mar 02 '17 at 18:11
  • Note that `inFile` is a variable, not a type. You can't send `inFile file` to a function - `inFile` is a variable name of type `ifstream`. – Ofer Arial Mar 02 '17 at 18:13
  • If you'd like to get a good answer, I suggest also appending the error messages the cocompiler shows you. In this case, you will also get hints for yourself from the error messages. – overseas Mar 02 '17 at 18:24
  • 1
    addendum to @OferArial 's comment correcting `inFile file` to `ifstream file` will also fail because it is [pass by value](http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) which will attempt to create `file` as a copy of the given parameter and `ifstream` cannot be copied. Pass it by reference. – user4581301 Mar 02 '17 at 18:40

4 Answers4

1

i will not write you the program, but let me point you in a direction.

first of all: please format your code better. maybe its just because stackoverflow, but there should be consistent and sensefull formatting of your code, otherwise you and others cant read it well and have trouble finding problems. (plz google clang-format for a tool, maybe an ide would do it as well).

split your programm in different logical parts. my approach would be:

  1. open file, if not possible give warning, end program
  2. read file content into a string IN: filename, OUT: string of content
  3. split the string into line, IN: string, OUT: std::vector
  4. for each line, split the line into parts(3 elements?) IN: linestring, OUT: name, street, ...
  5. You can that in proper datastructures, plz see the STL for good ones
  6. Access your datastructure for the wanted information.

Proposal for your data.

  • istream for file, you have that
  • std::string for file content
  • std::vector for line wise filecontent
  • std::unordered_map map the name to information.

this should give you a googling start. each subproblem can be solved independent (and tested) and is easier to find on SO :)

Good luck.

jonas_toth
  • 872
  • 5
  • 8
0

I agree with the comments. Yet to answer your issue "The function should read the first word of the file." below is a general idea how you can read the first word from file:

    ...
    ifstream inFile; // define your input stream
    string firstwrd; // first word

    inFile.open("yourfile"); // open your file
    inFile>> firstwrd; // this will read the first word
    inFile.close(); // close the file
    ...

And please consider reading through Jonas`s comments.

Edit: Please note my answer by no means is the definitive tutorial of best practices of reading from file. It applies your specific case.

HTH!

erol yeniaras
  • 3,701
  • 2
  • 22
  • 40
  • 1
    you can do the same thing with a stringstream. so when you would operate on an string, the same principle thing can be done, but you need another class – jonas_toth Mar 02 '17 at 18:31
  • @jonas_toth: stringstream is really a good option. Since similar questions have been asked and answered in StackOverflow, I just wanted to answer the specific question of getting the first word. – erol yeniaras Mar 02 '17 at 18:41
0

Before you can even begin to start programming, you have to identify the exact format of the file you want to read. That format gives you the order of operations for how you intend to read from the file.

In your example, you give:

Bob Smith 123456789
123 Main Street
Susan Smith 1224445555
543 Market Street

Which is broken down into a by-line format of:

[First Name] [Last Name] [User ID (I assume)]\n
[Address]

So now that we have that established, the first thing we do is open the file stream.

ifstream file("path\\to\\file");

When it comes to retrieving information from a file stream, there are 2 standard methods: the >> operator and getline().

The >> operator returns the very next block of text in a given fstream up to any whitespace character such as space, newline or return characters. The syntax for this is file >> var where file is the fstream you intend you read from and var is the variable you want to write to.

The getline() function will return the entire line, including spaces, but will stop at return and newline characters. The actual syntax of the function is std::getline(read, write); where read is the file stream or string you intend to actually read from and write is the variable you intend to copy the real line to.

For example:

ifstream file("file.txt");
string firstname, lastname, id, address;

file >> firstname; //get the first word of the file.
file >> lastname; //get the second word of the file.
file >> id; //get the third word of the file.

getline(file, address); //Get the next whole line of the file, regardless of how many words.

A funny quirk is that you don't have to worry about manually telling C++ where in the file you're wanting to look for the data. As the file is read a pointer is automatically kept inside of the file stream of where to begin reading from next. When you get one word, the pointer automatically starts at the beginning of the next word, so you just keep pulling data linearly until you reach the end of the file.

Kats
  • 143
  • 1
  • 12
0

void getFirstName(string f, inFile file); should be void getFirstName(string f, ifstream inFile);.

And remember, put an ampersand (&) between the type of the variable and the name to avoid creating a copy of the file (that consumes more ram), not putting the ampersand is only reasonable to use if you want to make changes to the variable that should not stick around.

And where is string f defined? You call the function without passing f but it's used in the function. That is a serious problem.

The ifstream.good() function if I remember correctly can't take parameters.

If you're trying to find f (you didn't tell us what it is so I can't be more precise) then you should first understand that f should be a file (since you used file.open(f);), pass the value to a string, and after doing inFile >> name_of_your_string; do if (name_of_your_string == f) { /* the last word read corresponds to f */}.

The type is ifstream, and the variable's name is inFile. Also since getFirstName is defined after main, you got to put this before main void getFirstName(string f, ifstream inFile); this is called a prototype, and it tells the compiler that the function is after main.

Obviously don't remove the rest of the function. If that's a problem for you move main under it. Also if someone puts a space in the input everything before the last space will be lost, remove cin >> file; and use getline (cin, file);

Remember to update the answer with more details on what f is and what exactly you want it to do.

EDIT: Remember to use inFile.close(); after you stop reading the file to avoid subtle errors.

Tommaso Thea
  • 542
  • 1
  • 10
  • 18