I'm wondering if there's a C++ way of opening a file and reading the input line by line.
I encountered the following code that accomplishes the task:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream myfile;
myfile.open ("example.txt");
return 0;
}
I'm encouraged to not use any C functions or commands.
The thing is, my "example.txt" is in the form of a string, and using str.c_str() is a C function, so I guess I have two ways to solve the issue.
Is there another way to read input from a file line by line? Perhaps using something that will accept a string as a parameter for the filepath? Is there a C++ way of doing things? :)
Or, is there another way to convert the string in to a const char *, which is what the myfile.open() function needs?
Many thanks in advance!
EDIT: My lack of practivity and research led me to think c_str() was a C function, and it isn't. My apologies. Since it isn't I have found my answer.
C++11's fstream constructor accepts string. In most cases, you want to use fstream's constructor, rather than .open() - you save one line and one function call.
For reading the file line-by-line, you should use std::getline().
Also note that string::c_str() is still C++ function, not C one, as well as fstream's constructor taking const char *. Most of (if not all, I'm not 100% sure) C standard library function are also included in C++ standard.
Since the issue about str.c_str() is already answered, I'm just gonna add a bit about getting inputs line by line. for example, you wanna take 2 ints input per line, extract them, and put it into a vector.
fstream fs(filename.c_str(), ios_base::in);
string line;
stringstream ss;
int a,b;
vector<int> d;
int numlines;
int i;
for (i = 0; getline(fs, line); i++) {
for (ss.str(line); ss >> a >> b; d.push_back(a), d.push_back(b)) {}
ss.clear();
}
numlines = i;
Hope you get the idea of using getline() and fstream()
It's going to look very similar. You'll want an ifstream instead of an ofstream, you'll want the >> operator, and assuming your file has more than one line, you'll need a loop and the ifstream::feof() function.
Related
I know I know.
This question has been asked before, but I've looked at all the answers and none seem to solve my problem. When I use the getline function to get the contents of a line in the file, it doesn't work.
getline(file, line);
'File' is declared here:
ifstream File;
File.open("fruit.txt");
and 'line' is declared here:
int line = 0;
Getline is underlined in red with this message:
getline
no instance of overloaded function "getline" matches the argument list
argument types are :(std::ifstream, int)
What this means is no instance of getline has the argument list of the file stream and an integer.
This makes no sense as all the other questions on this matter state exactly that, that the arguments are the file stream and an integer.
What am I doing wrong?
EDIT:
Here is the full code:
ifstream fruitFile;
fruitFile.open("fruit.txt");
int line = 0;
int C_FRUIT = getline(fruitFile, line);
fruitFile.close();
The first line should be a number, and I need it.
getline() will read one line of text. It can't read directly an int. This is why you get your error message.
You have to be aware that there are two getline(). There is one which is istream::getline() and std::getline(). Both have different signatures. The first is a member function of a stream and is defined in the stream header; the latter is defined in the <string> header.
But pay attention: the return value of std::getline() is not an int ! It's a stream reference. This is why you get a second compiler error.
Finally if you want to read an integer x, it's easier to use extractors:
int value;
fruitFile >> value;
fruitFile.ignore(SIZE_MAX, '\n'); // in case you'd need to go to next line
Or if you really want to read an int in a full line:
string line;
getline(fruitFile, line);
stringstream sst(line); // creates a string stream: a stream that takes line as input
sst >> value;
The second argument of getline needs to be a string: http://www.cplusplus.com/reference/string/string/getline/
I think what you try to achieve is:
ifstream fruitFile;
fruitFile.open("fruit.txt");
int line = 0;
fruitFile >> line
fruitFile.close();
I faced the same error. add this to your code to solve the problem
Add the string library
include <string>
Add the below function call, where string_variable should be of type string.
std::getline(cin, sting_variable)
I am new to C++ and I am trying to use the seekp function in order to return to the beginning of my binary file when reading. However, the Xcode compiler doesn't seem to recognize the seek function. The error given by the compiler is:
/Users/**/Desktop/Programming/Project/Project/Project.cpp:191:10: No member named 'seekp' in 'std::__1::basic_ifstream >'
What am I doing wrong?
Here is the code:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream f_in;
f_in.open("Pixmap.bin", ios::binary | ios::in);
ofstream f_out;
f_out.open("Pos.txt", ios::binary);
Handle_File_Errors(f_in, f_out);
Pixel_Counter(f_in);
f_in.seekp(0, ios::beg);
f_in.close();
f_out.close();
}
the Pixel_Counter function is written as follows:
void Pixel_Counter (ifstream &f_in) {
uint null;
int i=0;
for (i=0; !f_in.eof();i++) {
f_in.read((char*)&null, sizeof(null));
}
cout<<i-6<<endl;
}
Finally, the Handle_File_Errors does not do anything with the read function but simply checks if the ifstream and ofstream functioned correctly.
There is no seekp for istream.
You probably meant either seekg, or you wanted to use an ostream instead (which does have seekp).
Judging by your code, you seem to want input from the file, so seekg with your istream is appropriate here.
Bear in mind that ifstream and ofstream are subtypes of istream and ostream respectively.
In an ifstream you should use seekg() instead of seekp(), because you want to set the position for reading and not the position for writing.
ifstream and ofstream have only the one of the two function. An fstream can do both, but beware if it's for reading or writing. This tutorial (especially the section about random access) could be of interest for you.
I am trying to find a solution for the error that the c++ compiler (codeblocks) keeps showing to me,I searched for answers on the net but none of them seemed to be helpful.
I have a txt file in which there are numbers,each of them is written in a line.
What i want is to read each number(line by line)and store it in a variable after converting it from string to float.
This my code
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
float z;
int j=0;
stringstream s;
const ifstream fich1;
fich1.open("test.txt",ios::in);
if(!fich1)
{
cerr<<"could not open the file";
EXIT_FAILURE;
};
else const string ligne1;
while((!fich1.eof())
{
if(j!=i)
{
j++;
getline(fich1,l1); // the first error is: no matching function for call to ‘getline(const ifstream&, const string&)
else if(j == i)
{
fich1>>s; // the second error is: ambiguous overload for ‘operator>>’ (operand types are ‘const ifstream {aka const std::basic_ifstream<char>}’ and ‘std::stringstream {aka std::basic_stringstream<char>}’)
z=(float)s; // I couldn't find a clear answer to convert from string to float
}
}
if anyone wants to ask any question about the code to make it clearer may ask it please,I am waiting for your answers as well as your questions :)
After the edit, I am able to read some code, but still I am suggesting the example I have below, since I am see scary things, like an EOF inside a loop!
Oh also, if you have C++11 support, then you could use std::stof.
You could try this (since your post is not readable), which reads a file line by line and stores every float number in a vector:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
int main() {
std::vector<float> v;
std::string rec_file = "test.txt";
std::string line;
std::ifstream myfile(rec_file.c_str());
if(myfile.is_open()) {
while(std::getline(myfile,line)) {
if(!line.empty())
v.push_back(atof(line.c_str()));
}
myfile.close();
} else {
std::cerr << "Unable to open file";
}
for(unsigned int i = 0; i < v.size(); ++i)
std::cout << v[i] << std::endl;
return 0;
}
with the test.txt to be:
1.2
2.3
3.4
4.5
5.6
6.7
and the output:
1.2
2.3
3.4
4.5
5.6
6.7
Your ifstream shouldn't be constant, as getline alters the ifstream.
To convert from char array to float use atof(chararray)
To convert from string to float you could use atof(string.cstr())
You cannot use const here. But that's not your only problem.
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
Looks good.
float z;
int j=0;
stringstream s;
There should be some int main() { here, because that is the function called by the runtime when a C++ executable starts. This isn't Bash or Perl where execution simply picks up at the first statement.
And you either need to write using namespace std; or prefix identifiers like stringstream as std::stringstream.
const ifstream fich1;
This won't work. ifstream must not be declared const. (This is actually the explanation for the errors you encountered, but you made many more.)
An input file stream is an object with complex inner state, like where in the file you're currently at, error flags etc.; this inner state is changing so const ifstream simply won't work.
fich1.open("test.txt",ios::in);
This can't work either due to const, just like all the other operations on fich1 you're doing further down.
if(!fich1)
{ cerr<<"could not open the file";
EXIT_FAILURE;
};
EXIT_FAILURE is a symbolic constant for a return value. As-is, you could just as well have written 0;. If you expected this to end your program, you're wrong.
;
The semicolon ends the if.
else
Since the if has ended, this is a syntax error.
const string ligne1;
If you declare a string const, you cannot assign to it. Besides, even if the else were correct, that semicolon would have ended the else, because you didn't add braces (as you should always do, even with one-line blocks, because it's so easy to make mistakes otherwise).
The way the code continues, I very much doubt this was your intention. (Some discipline with the indenting makes this kind of mistakes really easy to spot.)
while((!fich1.eof())
How to read until EOF from cin.
{ if(j!=i)
{j++;
Do some proper indenting, will you? Besides, no i has been declared at this point.
getline(fich1,l1);
I assume l1 is the string you declared earlier as ligne1. (Since that happened inside the else, ligne1 is no longer in scope, but I'll let that one pass since you obviously intended the while to be inside the else block.)
Anyway, this can't work because both your ifstream and your string are constant, i.e. cannot be changed. This does not make sense, and thus getline() was not defined for const parameters.
else if(j == i)
Since you didn't close the brace of the if statement above, this is a syntax error as well. Again, proper indenting discipline would have made this immediately obvious.
{ fich1>>s;
I very much doubt there exists an operator>>() for const ifstream, so I am not surprised you get an error here.
z=(float)s;
You're trying to cast (C style, too...) a stringstream object to a float number? What do you expect the result might be? It will definitely not be the 3.14 or whatever you've written into test.txt...
}
}
Lost track of your braces. This code is FUBAR, I didn't even check if it makes sense semantically once all the errors are fixed, and I suspect you're pulling some elaborate troll prank here. If that isn't the case, I suggest you take another good look at whatever textbook you're using to learn C++, because you got a good many things very wrong.
Purely style-related advice: fich1, ligne1... don't use localized (non-English) identifiers. It just adds another problem when communicating about your code. (And this is coming from a non-native English speaker.)
This is so stupid. I've been stuck literally for an hour trying to read in a .txt file of numbers that are separated by a single whitespace. The while loops only gets executed once for some reason!
#include <iostream>
#include <string>
#include <fstream>
#include <stack>
using namespace std;
int main(int argc, char* argv[])
{
string line;
string str(argv[1]);
ifstream myfile((str).c_str());
int num;
stack<int> x;
while (myfile >> num);
{
x.push(num);
}
return(0);
}
Hmm, look at this line more closely:
while (myfile >> num);
Eventually, you'll notice the semi-colon. The compiler thinks this means you want a loop that does nothing (the semi-colon here indicates a single, empty statement). So, the loop reads in all the numbers, but does nothing with them.
The next section is interpreted separately as a statement in its own scope (denoted by the braces), to be executed after the loop:
{
x.push(num);
}
All that does is push the last number read onto the stack, leading you to think the loop only executes once.
Remove the ; and you're OK! Once bitten by this, you'll never forget ;-)
On an unrelated note, it's a bit silly to take argv[1] (a C-style string), put it into a string object, then use c_str() to turn that back into a C-string for the ifstream constructor. Just use argv[1] directly, since you're not doing anything else with it. Also, it would be a good idea to check argc first and make sure that a filename was passed in. Finally, you should check that the file was successfully opened instead of assuming it -- at the very least make your assumption explicit with an assert(myfile.is_open());. Oh, and you don't use the line variable at all.
I'm trying to get a part of text in a file.
I used "ifstream":
#include <fstream>
void func(){
char *myString;
ifstream infile;
infile.open("/proc/cpuinfo");
while (infile >> myString){
if (!strcmp(myString,"MHz"))
break;
}
}
and I get a "Segmentation fault". does anyone know why?
You have not allocated memory for the myString. Use std::string. Or better any other language, python, perl, or unix utils such as grep, awk, sed.
Because the target value should be:
std::string myString;
and not char*. It's possible to use char*, but you have to ensure that it points to something big enough first. (In your case, it doesn't point anywhere—you forgot to initialize it.) And defining “big enough” is non-trivial, given that you don't know the size of the next word until you've read it.
There's a reason why C++ has a string class, you know. It's because using char pointers is cumbersome and error-prone.
infile >> myString
will read from the file into *wherever myString points to. And it is an uninitialized pointer, it points to some random garbage address.
If you absolutely do want to use char pointers instead of strings, you'll have to allocate a buffer you can read data into.
But the sane solution is to replace it entirely by std::string.
Because you did not allocate memory for myString. The quick solution to this is to use std::string instead of the C-style char* strings, which does memory management so you don't have to.
Here's why your error occurs:
When you declare char *myString you are creating a pointer to a character. However you do not initialize this pointer to anything, so it can point absolutely anywhere in memory. When you do infile >> myString you are going to write a bunch of characters at an unspecified location in memory. It just so happens that this location was a vital part of your program, which caused it to crash.
char myString[256] compiles fine just as well too.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void func()
{
char myString[256] ;
ifstream infile;
infile.open("/proc/cpuinfo");
while ( ! infile.eof() )
{
infile >> myString;
cout<<myString<<" \n";
if (! strcmp(myString,"MHz") )
{
infile.close();
break;
}
}
infile.close();
cout<<" \n";
}
int main()
{
func();
return 0;
}