If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Reading a text file into a vector

I have an assignment where I have to write a program to prompt the user for a file name and location (it is a text file), once the user has entered that, a menu pops up and gives the user 4 options and asks which one they would like to do. Option 1 is to display all the names in the text file (there are 3 names, which have a first and last name with a space in between). The second option is to add a name to the file, the third option is to delete a name from the file and the fourth option is to exit and save the program, saving whatever names were added to or deleted from the file.

Now I have written out the code for the program, using 6 functions (including int main) the other five functions are: the read file into vector function, the display all names function, the add a name function, the delete a name function and finally the exit and save program function.

I seem to be having a problem with reading the names from the file into the vector, I am not exactly sure why either, seeing as I have followed how the teacher did it in class.

I will start by posting the readFile function and see if you guys can give me any help on it, I am desperate! I will take any help I can get, thank you!

while (oFile.fail()) //While the output file does not open display error message and prompt user again for a first and last name
{
cout << "----------------------------------------\n";
cout << "Output file error!\n";
cout << "Enter the name to be added <First and Last Name>: ";
cin >> strFName >> strLName;

oFile.open(strFile.c_str()); //Tries to open file again
}

cout << "----------------------------------------\n";
cout << "Enter the name to be added <First and Last Name>: "; //Asks user for the first and last name to be added
cin >> strFName >> strLName; //User enters first and last name to be added

vecNames.push_back(strFName+" "+strLName); //Adds the name entered into the vector

for (int i = 0; i < vecNames.size(); i++)
oFile << vecNames[i] << endl; //Put the names from the vector into the output file

oFile.close(); //Closes the output file

cout << "---- Name '" << strFName << " " << strLName << "' has been added.\n"; //Displays that the name has been added
}

cout << "----------------------------------------\n";
cout << "Enter the name to be deleted <First and Last Name>: "; //Ask use what name they want to delete
cin >> strFName >> strLName;

vecNames.pop_back(); //Take that name off of the vector

oFile.open(strFile.c_str()); //Open the output file

while (oFile.fail()) //While the output while does not open display and error message and ask the user again
{
cout << "----------------------------------------\n";
cout << "Output file error!\n";
cout << "Enter the name to be deleted <First and Last Name>: ";
cin >> strFName >> strLName;

oFile.open(strFile.c_str());
}

for (int i = 0; i < vecNames.size(); i++)
oFile << vecNames[i] << endl; //Put the names from the vector into the output file

void quitProgram() //Quitting the program function definition
{
cout << "--------------------------------------------------\n";
cout << "Thanks for using the program. Program terminated.\n"; //Displays that the program has terminated
}

Re: Reading a text file into a vector

...
I seem to be having a problem with reading the names from the file into the vector, I am not exactly sure why either, seeing as I have followed how the teacher did it in class...

Code:

void readFile(string strFile, vector<string> vecNames, ifstream &iFile) //Read the file into the vector function definition
{
string strFName, strLName; //First and last name
iFile.open(strFile.c_str()); //Opens file
while (iFile >> strFName >> strLName) //While the file is copying into the first and last names
{
vecNames.push_back(strFName + " " + strLName); //Push the names onto the back of the of the vector
}
iFile.close(); //Close the input file
}

Your very serious mistake is you are passing the vector<string> vecNamesby value, that means a copy of the vector is created within a function... but after function returns this copy is destroyed and all the read data gets lost!
You have to pass this vector by reference instead! (BTW, it is better to pass by const reference the string strFile parameter too):

Re: Reading a text file into a vector

Hey, sorry I wasn't sure how to do that on this site, I am sort of a noob with forums...
But thank you so much! I fixed those and now it's working great.

Although, the only problem I am having still is with my deleting a name function, in which I used pop.back, and it takes the last names off of the vector, I knew it wouldn't work but I wasn't sure how else to do it?. If I wanted to take a name off that is let's say in the middle of the vector, how would I do that?

Here is the code for the function:

Code:

void deleteName(string strFile, ofstream &oFile, vector<string> &vecNames) //Delete the name funtion definition
{
string strFName; //Initialize first name variable
string strLName; //Initialize last name variable
cout << "----------------------------------------\n";
cout << "Enter the name to be deleted <First and Last Name>: "; //Ask use what name they want to delete
cin >> strFName >> strLName;
vecNames.pop_back(); //Take that name off of the vector
oFile.open(strFile.c_str()); //Open the output file
while (oFile.fail()) //While the output while does not open display and error message and ask the user again
{
cout << "----------------------------------------\n";
cout << "Output file error!\n";
cout << "Enter the name to be deleted <First and Last Name>: ";
cin >> strFName >> strLName;
oFile.open(strFile.c_str());
}
for (int i = 0; i < vecNames.size(); i++)
oFile << vecNames[i] << endl; //Put the names from the vector into the output file
oFile.close(); //Close the output file
cout << "---- Name '" << strFName << " " << strLName << "' has been deleted.\n";
}

Re: Reading a text file into a vector

Hey, sorry I wasn't sure how to do that on this site, I am sort of a noob with forums...
But thank you so much! I fixed those and now it's working great.

You are still passing the string by value instead of const reference.

Although, the only problem I am having still is with my deleting a name function, in which I used pop.back, and it takes the last names off of the vector, I knew it wouldn't work but I wasn't sure how else to do it?.

First, when the name to delete is inputted, you should make sure that the name inputted is in the exact format as the names in the vector. In other words, you have to ensure that it's in this format:

Code:

strFName + " " + strLName;

Since that is how you are storing the names in the vector.

Once you ensure that the name inputted is in the same format, then the function you want to call is std::find(). This function returns an iterator to the found item, or vector::end() if not found. There is no need to write loops as you've done.

Then when it's found, then you call vector.erase() with the iterator returned from find.

The page above describes the algorithm find() function, and the vector::erase() function. Please look at the sample programs, and if you need to, write a simple main() program to get familiar with these two functions and the concepts of algorithms.

Re: Reading a text file into a vector

Purpose of MFC SDI: read a text file having floating values with tab separated not comma

I added a menu Contour plot menu and 1. colour and 2. Gray Scale as sub menu. On pressing Colour a dialog box appears, whose purpose is to browse a text file. I browse and selct it and the path is visible in an EDIT box beside the browse button
and a plot button in dialog box to plot on the SDI window.

I am successful in browsing and selecting the text file. When I click plot button it should read and further porcess it which i am not able to implement. I ahve the logic to process it. I need to read the text file and store the floating point values in an 2D array. Anyone can help me please. its urgent!!
Dialog box picture looks somewhat like this

ifs.open(m_path,ios::nocreate); //m_Path is a variable atached
//to edit box but i am not able
// to get the path of the txt fil
while (ifs.good())
{
ifs.getline(cNum,256,' ');
array[i]= atof(cNum);
i++;

Re: Reading a text file into a vector

Paul this user may want to know the internal workings of how to actually find a middle value in the vector not just to know that a function does it for you, in this case std::find, also the for loop will always work if you code it correctly??

Re: Reading a text file into a vector

Originally Posted by gaar321

Paul this user may want to know the internal workings of how to actually find a middle value in the vector not just to know that a function does it for you, in this case std::find, also the for loop will always work if you code it correctly??

Everything works if they're coded correctly. The keyword is if.

To ensure that there is no "if", find() makes sure there are no mistakes.

Re: Reading a text file into a vector

I found the solution the follwing codes reads the tab separated values and stores in a 2D vector and finds the max value in the 2d vector and divides them with largest value to all values in 2d vector. The fractional values which lies between0-1 is stored in text files with tab separated..
I am attaching the code

Re: Reading a text file into a vector

Ok Now after getting the output text file.. I want make a contour plot of the values. Th value are at each pixel i am having. I have the logic to convert the fractional values to RGB values and plot them pixel in view class.

My question is: Is it possible to generate a bitmap picture by using setpixel fucntion and save that bitmap as a temporary file in the working directory . display using CDC *pDC available fucntion to display bitmap generated in the working directory inthe ondraw() function. It should automatically take the bitmap from the working directory.. What is the way to catch the bitmap directory automatically?

It should e done when i press PLOT BUTTON... Please anyone caould help me out??? Please