| | |
opening file and putting into program
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 } }
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
![]() |
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
Views: 371 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






