How can I write a function to open a file (say "C:\list.txt") and add each item in the file to an array using a newline as a deliminator?
Printable View
How can I write a function to open a file (say "C:\list.txt") and add each item in the file to an array using a newline as a deliminator?
I think that will work.Code:#include <iostream.h>
#include <fstream.h>
int main()
{
ifstream InFile("list.txt", ios::nocreate);
char mylist[100][100];
for(int i=0; i<=99; i++)
{
InFile>>mylist[i];
}
return 0;
}
Thanks. It gave me a good start.
The standard way ;)
Code:#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
ifstream input("list.txt");
if(!input)
return -1;
vector<string> lines;
string temp;
while(!input.eof()) {
getline(input, temp);
lines.push_back(temp);
}
cout << "There are " << lines.size() << " lines." << endl;
}
I was actually thinking you'd fix that when I was typing fstream.h :p
I love being predictable ;)
PS: If you've got a copy of VC++7 anywhere, try compiling your code and watch the warning messages :D