| | |
opening file and putting into program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
I'm writing a program that opens a file then sorts it a couple different ways then closes it. Someone in my class said something about using a getline function but I can't figure out how that works. I can't find a way to take a specific line in a text file and then put it into an array. How can I get each number into its own array?
4532 111 2123
324 3211 223
233 4493 992
4532 111 2123
324 3211 223
233 4493 992
>>but I can't figure out how that works.
>>I can't find a way to take a specific line in a text file
You have to read each line one at a time until you get to the line that you want.
>>How can I get each number into its own array?
What kind of array? int array?
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <string> using namespace std; int main() { ifstream in("filename.txt"); string line; // read each line of the file while( getline( in, line) ) { //blabla do something this line } }
>>I can't find a way to take a specific line in a text file
You have to read each line one at a time until you get to the line that you want.
>>How can I get each number into its own array?
What kind of array? int array?
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <string> #include <vector> using namespace std; int main() { vector<int> arry; // array of integers ifstream in("filename.txt"); int num; // read each line of the file while( line >> num ) { arry.push_back(num); // add the number to the array } }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- error in guessing game (Python)
- pls help (C++)
- xp remote desktop across wan/lan (Windows NT / 2000 / XP)
- Paint shop Pro 7 (Graphics and Multimedia)
- putting password on files (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: another alg. problem...
- Next Thread: Create "Help" in application
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






