The issue is I've this file but I don't how obtain the string or numbers on it without quotes or commas ,and put it in auxiliar variable,
"string one" , 2500:25670, (0.676,-2.43)
"string two",259: 8765 , ( 12.22 , -7.56 )
For the moment I wrote this code:
string filename = getFilename(); //function to get filename
string line;
string data;
ifstream file;
file.open(filename.c_str(),ios::in);
if (!file.is_open())
{
error(ERR_CANT_OPEN_FILE);
}
else
{
while(getline(file,line))
{
bases.push_back(tempBases));
}
}
In the loop of reading file line by line:
while(getline(file,line))
{
// parse line to a token of strings with comma delimiter and assign each token to variables or put to the vector
}
Related
I am trying to read from a file like this
1 23 5 15
3 18 8 6 11
But then whenever it goes to the last string "15", it also reads "3" from then next line.
Here is my code for this part:
ifstream myFileRent;
myFileRent.open("rented.txt");
if (!myFileRent.is_open())
{
cout << "ERROR: File is corrupted or does not exists!";
}
while (!myFileRent.eof())
{
getline(myFileRent, customerID, ' ');
while (getline(myFileRent, video_ID, ' '))
{
InsertCusRent(stoi(customerID), video_ID);
}
}
This is what it shows in debug when I am in the "15" before the next line "3"
video_ID : "15\n3" : std::string
Basically, first digit of the line will go to customerID, and every next digits will be pushed in to stack, which is why I used while because every line are not equal in length.
Thanks for every comments, code is finally working!
string line, customerID, video_ID;
ifstream myFileRent;
myFileRent.open("rented.txt");
if (!myFileRent.is_open())
{
cout << "ERROR: File is corrupted or does not exists!";
}
while (!myFileRent.eof())
{
getline(myFileRent, line);
stringstream ss(line);
getline(ss, customerID, ' ');
while (getline(ss, video_ID, ' '))
{
if (customerID != " ")
{
InsertCusRent(stoi(customerID), video_ID);
}
}
}
I would do it like this:
string line;
ifstream myFileRent;
myFileRent.open("rented.txt");
if (!myFileRent.is_open())
{
cout << "ERROR: File is corrupted or does not exists!";
}
// Don't use eof here.
// Even if it is not eof does not mean that there is data to read.
// The next read may still fail. So try and read the data if it
// fails the while loop will not enter.
while (getline(myFileRent, line))
{
// You have a line. Lets use it in a stream.
stringstream ss(std::move(line));
// You convert customerID into an integer. Just
// make it an integer now.
int customerID;
std::string video_ID;
// Use the operator>> to read the customer ID into an integer.
ss >> customerID;
// To read simple white space separated values use operator>>
// It will read ints or string as appropriate.
while (ss >> video_ID)
{
InsertCusRent(customerID, video_ID);
}
}
PS. You can get your WORKING code reviewed once working at https://codereview.stackexchange.com/
I have a txt file that contains individual characters line by line. I am kind of confused as to how to read that line by line when the getline function takes in string?
Also would it be possible to store char in a vector? Or would it still work if I stored those individual characters as strings?
code show as below:
vector<char> res;
int count = 0;
ifstream fin;
fin.open("***.txt");
string str;
while (!fin.eof())
{
getline(fin, str);
res[count++] = str;
}
fin.close();
I want to read string in a file using c++.
The input string like: "File: blacksburg.asc"
I need to read string after ":"
Part of my code as following:
void mapWalk( string input, string output ){
ifstream in(input.c_str());
string line;
string mapdata = "";
getline(in,line);
int pos = line.find(":");
mapdata = line.substr(pos);
cout<<"The string is"<< mapdata <<"&&" << endl;
}
The output is shown as following:
&&e string is: blacksburg.asc
It is so wired!!
Why && is at begin of the string and cover the other?
Does someone have ideas?
There's a carriage return character at the end of mapdata. So you output "The string is", then the contents of mapdata including the carriage return. Then you output && on top of the previous text because you returned the carriage without a line feed.
Read the documentation for getline. It specifies that the line ending is not removed.
I have a csv file of atomic elements with atomic number, symbol, and name. The file is formatted as:
1,H,Hydrogen
2,He,Helium
3,Li,Lithium
...
I'd like to create an array of the symbols referenced by the atomic number. ie. ArrayName[32]="Ge";
I've been trying to use sscanf but it hasn't been working. rough code below:
char temp[200];
float temp_z;
std::string temp_ele;
std::string temp_name;
while(!fin.eof())
{
fin.getline(temp,200);
sscanf(temp, "\"%f\",\"%s\", \"%s\"",&temp_z, &temp_ele, &temp_name);
cout<<temp_z<<endl;
cout<<temp_ele<<endl;
cout<<temp_name<<endl;
}
Read every line of your file with this loop :
string line;
ifstream myfile;
myfile.open("myfile.txt");
if(!myfile.is_open()) {
perror("Error open");
exit(EXIT_FAILURE);
}
while(getline(myfile, line)) {
// Split line by comma to get what's your want
}
Then split every line by comma to get every element of the line.
You can read each element like so:
string theStrings[200]; //initialize to correct size
int i = 0;
string name;
while(!fin.eof())
{
getline(thefilestream, name, ',' );
theStrings[i++] = name;
cout<<name<<endl;
}
I'm trying to change this function to also account for when CSV files are given with \r endings. I can't seem to figure out how to get getline() take that into account.
vector<vector<string>> Parse::parseCSV(string file)
{
// input fstream instance
ifstream inFile;
inFile.open(file);
// check for error
if (inFile.fail()) { cerr << "Cannot open file" << endl; exit(1); }
vector<vector<string>> data;
string line;
while (getline(inFile, line))
{
stringstream inputLine(line);
char delimeter = ',';
string word;
vector<string> brokenLine;
while (getline(inputLine, word, delimeter)) {
word.erase(remove(word.begin(), word.end(), ' '), word.end()); // remove all white spaces
brokenLine.push_back(word);
}
data.push_back(brokenLine);
}
inFile.close();
return data;
};
This is a possible duplicate of Getting std :: ifstream to handle LF, CR, and CRLF?. The top answer is particularly good.
If you know every line ends with a \r you can always specify the getline delimiter with getline(input, data, '\r'), where input is an stream, data is a string, and the third parameter is the character to split by. You could also try something like the following after the start of the first while loop
// after the start of the first while loop
stringstream inputLine;
size_t pos = line.find('\r');
if(pos < line.size()) {
inputLine << std::string(x.begin(), x.begin() + p);
inputLine << "\n"
inputLine << std::string(x.begin() + p + 1, x.end());
} else {
inputLine << line;
}
// the rest of your code here