This question already has answers here:
c++: ifstream open problem with passing a string for text file name [duplicate]
(3 answers)
Closed 10 years ago.
My code should open 100 files (and do something with them) with next indexes in path like:
"c:\Naprzeme\NAPRZ100.IN" next one is "c:\Naprzeme\NAPRZ101.IN" and etc. :
for (int as=100;as<159;as++){
ostringstream ss;
ss << as;
string cherk = ss.str();
string supremeCounter = "c:\\Naprzeme\\NAPRZ"+cherk+".IN";
fstream infile(supremeCounter);
//....other code here
}
and fstream infile(supremeCounter) returns error
28 31 C:\Users\talent\Documents\File.cpp [Error] no matching
function for call to 'std::basic_fstream<char>::basic_fstream(std::string&)'
candidates are: //(here some libs)...
fstream infile(supremeCounter.c_str());
Related
This question already has answers here:
Put A String In A ifstream Method [duplicate]
(2 answers)
No matching function - ifstream open()
(1 answer)
Closed 3 years ago.
im new to c++ and trying to put a variable in this line : ifstream studentPaper(paper);
ill pass paper to this function and want to use it there. string paper has my files location (/name/file.txt)
if i put my file name there i dont get any errors = ifstream studentPaper("/name/file.txt");
but when i save my files location in to a string and give string to it ill get error = ifstream studentPaper(paper);
how can i do that without getting errors
void matchGrades(string paper) {
string aa= "asd";
ifstream studentPaper(paper);
ifstream base("base.txt");
int grade=3;
while ((!studentPaper.eof()) && (!base.eof())) {
string l1,l2;
getline(studentPaper,l1);
getline(base,l2);
if(l1==l2){
grade += 3;
} else {
grade -= 3;
}
}
studentPaper.close();
base.close();
cout << grade;
I think that You have to use removed string parameter "/name/file.txt" because parameter split space.
Try doing ifstream studentPaper(paper.c_str()).
Also if your file is located where your main.cpp is you won't need to specify the path. Something like this:
string studentFile = "student_file.txt";
Based on the information provided. If you are still getting an error please post it so that I can adjust my answer.
This question already has answers here:
How can I read and parse CSV files in C++?
(39 answers)
Closed 6 years ago.
I am currently trying to read in a CSV file to place it into an array, but when I execute the code, the program seems to read over the endline to the next comma which messes up my output. Here is the code:
while (!inFile.eof()) {
string line = "";
while (count_1 <= numValuesPerLine) {
getline(inFile, readFromFile, ',');
line.append(readFromFile);
count_1++;
}
cout << line << endl;
count_1 = 0;
}
'line' ends up having the value:
12345678910111213141516171819202122232425\n1
which when I print it, places that newline next to '25' and messes up the output.
(numValuesPerLine = 25 and count_1 is initialized outside of the loop)
I looked around for a similar answer but I could not find anything exactly like what I am trying to do, any help would be greatly appreciated, thank you.
you changed the delimiter from \n to , so of course the newline is kept as part of the input
This question already has answers here:
How to check if string ends with .txt
(12 answers)
Closed 8 years ago.
A file name is being passed down into a function. The string needs to be checked to make sure it ends with ".bmp"
How do i check for that to later open the file?
For example: if a string contains "picture.txt" it will tell the user the string is not in the correct format. If a string contain "picture.bmp" it should accept it for later use of opening the file.
Thanks in advance!
What OS are you using? If it's Windows / Visual C++, you have functions that properly give you the extension given a file name (for example _spiitpath). If you want something portable, you have boost::filesystem to parse out the name.
The reason why you should use these functions instead of trying to cook something up yourself is that there could be corner or edge cases that you didn't consider, thus causing your code to give wrong results.
You can do something like this:
bool hasExtension(const string& filename, const string& extension)
{
size_t fnlen = filename.length();
size_t exlen = extension.length();
if (fnlen < exlen)
return false;
return memcmp(filename.c_str() + fnlen - exlen, extension.c_str(), exlen) == 0;
}
int main()
{
cout << hasExtension("file.txt", ".txt") << endl;
}
This question already has answers here:
How do you clear a stringstream variable?
(9 answers)
Best way to empty stringstream?
(1 answer)
Closed 9 years ago.
How do I clear a string stream. I am tokenizing a file line by line, and the stringstream is global. I can tokenize the first line, but I can't get the second line working
stringstream currentLine;
while (!inputFile.eof())
{
currentLine << getlinefromfile(); //Gets the next Line
tokenizeLine(); //Tokenizes the line using the global stringstream peek() fnc
currentLine.str("");
currentLine.clear();
}
The above is my current attempt, but it doesnt work.
This question already has answers here:
How can I read and parse CSV files in C++?
(39 answers)
Closed 8 years ago.
What is the best way to read in a tab delimited file in C++ and store each line as a record? I have been looking for an open source library to help with this, but have been unsuccessful so it looks like I will have to write my own.
typedef vector<vector<string> > Rows;
Rows rows;
ifstream input("filename.csv");
char const row_delim = '\n';
char const field_delim = '\t';
for (string row; getline(input, row, row_delim); ) {
rows.push_back(Rows::value_type());
istringstream ss(row);
for (string field; getline(ss, field, field_delim); ) {
rows.back().push_back(field);
}
}
This will get you started. It doesn't do any checking that each row has the same number of fields, allow for escaping field_delim, etc.
There is no problem in using iostreams - you could read each line with getline into string, and then use stringstream on that string to iterate over fields.
There are a few libraries listed in wikipedia's article CSV_application_support.