ifstream fileName;
fileName.open("file.txt");
string data; // it could be int, or any data type you want
vector<string>myVec; // if you data type is 'int', do replace <string> with <int>
while (!file.eof()) // loop until end of file
{
fileName >> data;
myVec.pushback(data);
}
This is the simplest way to read from file and store data in vector.
Tip: Do not forget to #include <vector> before your main
(Or did you mean that "vector" was an array of int? e.g. int vector[4]; ?? If so, it's a confusing name to use as it's the name of a standard C++ container class -- std::vector.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std;
// etc
vector<int> myVector; // for example
// add some value to vector
ofstream myfile("file.txt");
// check it opened OK
for(int i = 0; i < myVector.size(); i++) {
.....myfile << myVector[i] << endl;
}
// Etc
PART 2
You can do something like this: ...
You shouldn't really use fstream::eof() to control a read loop!!
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
usingnamespace std;
int main()
{
// It's better form to use constructor that opens file
// rather than the default constructor followed by open
// method. Destructor calls close(), of course.
ifstream fileIn("file.txt"); // fileName is not a good name for a file!
if(!fileIn.is_open())
{
cout << "Failed to open file!\n";
return 0;
}
string data; // it could be int, or any data type you want
vector<string> myVec; // if you data type is 'int', do replace <string> with <int>
// operator>> returns a reference to the fstream which
// can be tested using if(), etc to see if its state is still good
// (this checks not only for eof() but also error conditions.
while (fileIn >> data) // loop until no more input or error
{
// and remember operator>> can only extract string without spaces!
myVec.push_back(data);
}
// Etc (could check for eof() here to confirm whole file was processed.)
return 0;
}
Ok, thanks! Now I have another question... I wrote some data to a file, but every time I run the program it erases the content of the file and writes new data.
I've already tried with FILE *ยจ, but I can't figure out how to use fwrite or fprintf correctly... Can you explain to me with a piece of code, how you'd write the data of a string vector to a file?
O better, without using FILE *, how can I write data to a file, without erasing what it was already written in it?
ofstream myFile;
myFile.open("data.txt", fstream::app); // by opening it with this statement, any data you write
// to the file will be appended to the file instead of overwriting
Let's supose I have: vector<string> nombres;
And then, the user introduces a name: cin >> nombre;
How can I check inmediately if that is inside the vector?
I'm looking for something like in Python, in which we only write: if(nombre in nombres): ...
Something like that, is it possible?
And, another thing, how can I show the content of a txt file in the console, directly as it is in the file?