|
-
Jul 29th, 2002, 05:49 PM
#1
Thread Starter
Stuck in the 80s
Open File
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?
-
Jul 29th, 2002, 06:00 PM
#2
Frenzied Member
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;
}
I think that will work.
-
Jul 30th, 2002, 12:32 AM
#3
Thread Starter
Stuck in the 80s
Thanks. It gave me a good start.
-
Aug 2nd, 2002, 11:20 AM
#4
Monday Morning Lunatic
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 refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 2nd, 2002, 01:54 PM
#5
Frenzied Member
I was actually thinking you'd fix that when I was typing fstream.h
-
Aug 2nd, 2002, 03:08 PM
#6
Monday Morning Lunatic
I love being predictable 
PS: If you've got a copy of VC++7 anywhere, try compiling your code and watch the warning messages
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|