Having trouble parsing simple text file separated by commas [duplicate] - c++

I'm trying to read a list of items from from a file and then store them into a vector. The issue is my code is adding the last item to the vector twice and I'm not sure why the it keeps reading the file even though the program has reached the end.
Here's what's in the text file. The "Oranges" line appears twice when I display the contents of the vector.
Apples-pounds-10 2
Oranges-pounds-5 6
Here's the code
//Read the contents of the list to a file
while (!inputFile.fail())
{
//Extract the line from the list
getline(inputFile,item_name,'-');
getline(inputFile,item_unit,'-');
inputFile >> item_amount;
inputFile >> item_price;
//Create an instance of the item object
Item New_Item(item_name, item_unit, item_amount,item_price);
//Push it to the list vector
list.push_back(New_Item);
}
//Close the file
inputFile.close();

This is a typical symptom of the while (!infile.fail()) anti-pattern.
I'd define a struct and overload operator>> for that type:
struct item {
std::string name;
std::string unit;
int amount;
int price;
};
std::istream &std::operator>>(std::istream &is, item &i) {
getline(is, i.name, '-');
getline(is, i.unit, '-');
is >> i.amount;
return is >> i.price;
}
With those defined, reading the data borders on trivial:
std::ifstream inputFile("fileNameHere");
std::vector<New_Item> items { std::istream_iterator<Item>(inputFile),
std::istream_iterator<Item>() };
[I changed it from list to vector, because, well, you really don't want list. You can change it back, but probably shouldn't.]

The problem is that the "fail" flag is not set until you make an attempt at reading some more data from the file. Here is a quick way of fixing this:
for (;;) {
//Extract the line from the list
getline(inputFile,item_name,'-');
getline(inputFile,item_unit,'-');
inputFile >> item_amount;
inputFile >> item_price;
if (inputFile.fail()) break;
//Create an instance of the item object
Item New_Item(item_name, item_unit, item_amount,item_price);
//Push it to the list vector
list.push_back(New_Item);
}
If this is for a learning exercise, and you have not learn the >> operator yet, this should do it. Otherwise, the operator>> approach is better.

Related

My alphabetical string organizer won't save into a file correctly

I have a function that I want to take a file, look at the words in the file, position them in alphabetical order and replace the file with that. So far, I have it to get the words in alphabetical order. The problem is, is that it only saves the last word to the file.
Here is my current code:
void thingy(string Item)
{
fstream File; // Open the file, save the sorted words in it, and then close it.
File.open("Data/Alphabet.txt", ios::out | ios::in);
File << Item;
File.close();
}
void Alphabetical_Order(string Text_File)
{
fstream file;
file.open(Text_File); // Open the file that we are going to organize
std::set<std::string> sortedItems; // The variable that we will store the sorted words in
fstream words; // To see how many words are in the file
words.open(Text_File);
int Words = 0;
do
{
string word;
words >> word;
Words++;
} while (!words.eof());
for (int i = 1; i <= Words; ++i) // Set a loop to take the words out of the file and put them in our variable
{
std::string Data;
file >> Data;
Data = Data + " ";
sortedItems.insert(Data);
}
std::for_each(sortedItems.begin(), sortedItems.end(), thingy);
}
Does anyone know how to fix this?
When you open the fstream in thingy, try opening with ios::ate flag as well. This will allow you to append your text to the file, rather than rewrite every time you call the function.
That being said, you should not be opening and closing the file every time you call the function. Maybe pass a reference to a fstream that gets managed from outside the function thingy.
I hope this helps.
Aiden already pointed out the main problem in your code (+1).
For the records, I'd like however to propose you a more efficient output using the powerfull ostream_iterator: it avoids opening/closing the output file a lot of times and doesn't need trailing spaces in all strings. This being said, I'd suggest to void the unnecessary double pass read as well:
void Alphabetical_Order(string Text_File)
{
ifstream file(Text_File); // Open in read mode
std::string word;
std::set<std::string> sortedItems; // The variable that we will store the sorted words in
while (file >> word) { // just read the file in one pass
sortedItems.insert(word); // to populate the set
}
ofstream ofs("alphasorted.txt");
std::copy(sortedItems.begin(), sortedItems.end(), std::ostream_iterator<std::string>(ofs, " "));
}

could'nt read correct values of vector produced from file

I am trying to read text file line by line and then read each column as vector but when i am tryin to cout first column it shows zeros i.e. not reading the file correctly.
int main(void)
{
ifstream myfile ("data1.txt");
string line;
if (myfile.is_open())
{
int ln=1;
while ( getline (myfile,line) )
{
if(ln==1){ln++; continue;}
istringstream iss(line);
string word;
vector<double> column;
int w=1;
while(iss >> word)
{
//double dw=atof(Form("%s",word));
column.push_back(atof(Form("%s",word)));
cout<<column[0];
w++;
}
ln++;
cout<<"\n";
}
myfile.close();
}
//else
else cout<<"Unable to open file";
cout<<"\n";
return ;
}enter code here
push_back appends an element as last element of the vector while columns[0] always refers to the first element of the vector.
Is the first element 0
Is there another problem?
(Please explain what is Form, give an example of input file and output in the command line)
First of all learn how to indent and consistently use some scheme for inserting blank lines that makes sense. When you do that you can read your own code and figure out if it is doing what you think it is.
Second. Save Form("%s",word) in a string ( for now call it form_string) add this line cout<<"form returns "<<form_string<<endl; 99.99% probably it will print zeros.
Finally change: cout<<column[0]; to cout<<column[0]<<" "; or cout<<*(column.rbegin())<<" ";. The latter prints out all the values that you read, the former prints out the first value you read over and over.

Reading data from a file and storing it into a vector

I'm trying to read a list of items from from a file and then store them into a vector. The issue is my code is adding the last item to the vector twice and I'm not sure why the it keeps reading the file even though the program has reached the end.
Here's what's in the text file. The "Oranges" line appears twice when I display the contents of the vector.
Apples-pounds-10 2
Oranges-pounds-5 6
Here's the code
//Read the contents of the list to a file
while (!inputFile.fail())
{
//Extract the line from the list
getline(inputFile,item_name,'-');
getline(inputFile,item_unit,'-');
inputFile >> item_amount;
inputFile >> item_price;
//Create an instance of the item object
Item New_Item(item_name, item_unit, item_amount,item_price);
//Push it to the list vector
list.push_back(New_Item);
}
//Close the file
inputFile.close();
This is a typical symptom of the while (!infile.fail()) anti-pattern.
I'd define a struct and overload operator>> for that type:
struct item {
std::string name;
std::string unit;
int amount;
int price;
};
std::istream &std::operator>>(std::istream &is, item &i) {
getline(is, i.name, '-');
getline(is, i.unit, '-');
is >> i.amount;
return is >> i.price;
}
With those defined, reading the data borders on trivial:
std::ifstream inputFile("fileNameHere");
std::vector<New_Item> items { std::istream_iterator<Item>(inputFile),
std::istream_iterator<Item>() };
[I changed it from list to vector, because, well, you really don't want list. You can change it back, but probably shouldn't.]
The problem is that the "fail" flag is not set until you make an attempt at reading some more data from the file. Here is a quick way of fixing this:
for (;;) {
//Extract the line from the list
getline(inputFile,item_name,'-');
getline(inputFile,item_unit,'-');
inputFile >> item_amount;
inputFile >> item_price;
if (inputFile.fail()) break;
//Create an instance of the item object
Item New_Item(item_name, item_unit, item_amount,item_price);
//Push it to the list vector
list.push_back(New_Item);
}
If this is for a learning exercise, and you have not learn the >> operator yet, this should do it. Otherwise, the operator>> approach is better.

Reading in from a txt file. Trouble parsing info

I want to read in scores from a txt file. The scores are going into a struct.
struct playerScore
{
char name[32];
int score, difficulty;
float time;
};
the text file looks like this
Seth 26.255 40 7
as one line, where each item is followed by a tab. (Name\t time\t score\t difficulty\n)
When I begin to read in the text, I don't know how to tell the program when to stop. The scores file could be any number of lines or score entries. This is what I have attempted.
hs.open("scores.txt", ios_base::in);
hs.seekg(0, hs.beg);
if (hs.is_open())
{
int currpos = 0;
while (int(hs.tellg()) != int(hs.end));
{
hs>> inScore.name;
hs >> inScore.time;
hs >> inScore.score;
hs >> inScore.difficulty;
hs.ignore(INT_MAX, '\n');
AllScores.push_back(inScore);
currpos = (int)hs.tellg();
}
}
I'm trying to make a loop that will read in a line of code into a temp struct for the data, then push that struct into a vector of structs. Then update the currpos variable with the current location of the input pointer. However, the loop just gets stuck on the condition and freezes.
There are a multitude of ways to do this, but the following is likely what you're looking for. Declare a free-operator for extracting a single-line definition of a player-score:
std::istream& operator >>(std::istream& inf, playerScore& ps)
{
// read a single line.
std::string line;
if (std::getline(inf, line))
{
// use a string stream to parse line by line.
std::istringstream iss(line);
if (!(iss.getline(ps.name, sizeof(ps.name)/sizeof(*ps.name), '\t') &&
(iss >> ps.time >> ps.score >> ps.difficulty)))
{
// fails to parse a full record. set the top-stream fail-bit.
inf.setstate(std::ios::failbit);
}
}
return inf;
}
With that, your read code can now do this:
std::istream_iterator<playerScore> hs_it(hs), hs_eof;
std::vector<playerScore> scores(hs_it, hs_eof);
I dont think that you can just >> from your file. Do you think it will take everything till \t? :)
You can try to take for example token with strtok()
I guess it can use '\t' to split string and take for each variable via this function needed part of string
In case if it strtok() doesnt work that way i guess you can just copy till '\t' in sub-loop
You can do like this
playerScore s1;
fstream file;
file.open("scores.txt", ios::in | ios::out);
while(!file.eof()) //For end of while loop
{
file.read(s1, sizeof(playerScore));//read data in one structure.
AllScores.push_back(s1);
}

Reading in a .txt file word by word to a struct in C++

I am having some trouble with my lab assignment for my CMPT class...
I am trying to read a text file that has two words and a string of numbers per line, and the file can be as long as anyone makes it.
An example is
Xiao Wang 135798642
Lucie Chan 122344566
Rich Morlan 123456789
Amir Khan 975312468
Pierre Guertin 533665789
Marie Tye 987654321
I have to make each line a separate "student", so I was thinking of using struct to do so, but I don't know how to do that as I need the first, last, and ID number to be separate.
struct Student{
string firstName;
string secondName;
string idNumber;
};
All of the tries done to read in each word separately have failed (ended up reading the whole line instead) and I am getting mildly frustrated.
With the help from #Sylence I have managed to read in each line separately. I am still confused with how to split the lines by the whitespace though. Is there a split function in ifstream?
Sylence, is 'parts' going to be an array? I saw you had indexes in []'s.
What exactly does the students.add( stud ) do?
My code so far is:
int getFileInfo()
{
Student stdnt;
ifstream stdntFile;
string fileName;
char buffer[256];
cout<<"Please enter the filename of the file";
cin>>filename;
stdntFile.open(fileName.c_str());
while(!stdFile.eof())
{
stdFile.getLine(buffer,100);
}
return 0;
}
This is my modified and final version of getFileInfo(), thank you Shahbaz, for the easy and quick way to read in the data.
void getFileInfo()
{
int failed=0;
ifstream fin;
string fileName;
vector<Student> students; // A place to store the list of students
Student s; // A place to store data of one student
cout<<"Please enter the filename of the student grades (ex. filename_1.txt)."<<endl;
do{
if(failed>=1)
cout<<"Please enter a correct filename."<<endl;
cin>>fileName;
fin.open(fileName.c_str());// Open the file
failed++;
}while(!fin.good());
while (fin >> s.firstName >> s.lastName >> s.stdNumber)
students.push_back(s);
fin.close();
cout<<students.max_size()<<endl<< students.size()<<endl<<students.capacity()<<endl;
return;
}
What I am confused about now is how to access the data that was inputted! I know it was put into a vector, but How to I go about accessing the individual spaces in the vector, and how exactly is the inputted data stored in the vector? If I try to cout a spot of the vector, I get an error because Visual Studio doesn't know what to output I guess..
The other answers are good, but they look a bit complicated. You can do it simply by:
vector<Student> students; // A place to store the list of students
Student s; // A place to store data of one student
ifstream fin("filename"); // Open the file
while (fin >> s.firstName >> s.secondName >> s.idNumber)
students.push_back(s);
Note that if istream fails, such as when the file finishes, the istream object (fin) will evaluate to false. Therefore while (fin >> ....) will stop when the file finishes.
P.S. Don't forget to check if the file is opened or not.
Define a stream reader for student:
std::istream& operator>>(std::istream& stream, Student& data)
{
std::string line;
std::getline(stream, line);
std::stringstream linestream(line);
linestream >> data.firstName >> data.secondName >> data.idNumber;
return stream;
}
Now you should be able to stream objects from any stream, including a file:
int main()
{
std::ifstream file("data");
Student student1;
file >> student1; // Read 1 student;
// Or Copy a file of students into a vector
std::vector<Student> studentVector;
std::copy(std::istream_iterator<Student>(file),
std::istream_iterator<Student>(),
std::back_inserter(studentVector)
);
}
Simply read a whole line and then split the string at the spaces and assign the values to an object of the struct.
pseudo code:
while( !eof )
line = readline()
parts = line.split( ' ' )
Student stud = new Student()
stud.firstName = parts[0]
stud.secondName = parts[1]
stud.idNumber = parts[2]
students.add( stud )
end while