| | |
inputing a text file into an array
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved | View First Unread |
you don't need lines 25-29 to zero out the array. Do it when the array is declared, like this:
>>What alterations/additions do I make to the code to import the .txt file into the array?
Code that reads the text file. Start by deleting lines 1 to 3 -- al those files are obsolete in C++. Most likely all you need is this:
Next declare an ifstream object Then in a loop read the integers into the array. See Vernan's post #6 above, which is an example of how to read the file.
int matrix[101][5] = {0}; >>What alterations/additions do I make to the code to import the .txt file into the array?
Code that reads the text file. Start by deleting lines 1 to 3 -- al those files are obsolete in C++. Most likely all you need is this:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std;
Next declare an ifstream object Then in a loop read the integers into the array. See Vernan's post #6 above, which is an example of how to read the file.
Last edited by Ancient Dragon; Jan 27th, 2008 at 6:09 pm.
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.
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Solved Threads: 0
Sadly, the c++ compiler i have to use at uni must use those headers, as it isn't a very new program. So I do have to use them 
You say to declare an ifstream object and then in a loop, read the integers into an array.
I know this is what I must do, but this is the code I am struggling with... I've never come across any examples of this before. Could you possibly give me some brief code that will read the integers into the array?
Thanks for the help with zeroing, that worked a treat

You say to declare an ifstream object and then in a loop, read the integers into an array.
I know this is what I must do, but this is the code I am struggling with... I've never come across any examples of this before. Could you possibly give me some brief code that will read the integers into the array?
Thanks for the help with zeroing, that worked a treat
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
So you do know that you have three columns? You don't know how many rows, but there are three columns? If you don't know how many columns, this won't work. If it's different from 3, change numCols. Anyway, here's a way to read it in:
You have
at the top before anything is read in, so you want to take that out. I think these lines are going to give you an error:
You only have one "%d", but you have two parameters. I'd take the "k" parameter out.
C++ Syntax (Toggle Plain Text)
int numCols = 3; int numRows = 0; while (inFile >> matrix[numRows][0]) { printf ("%d\t", matrix[numRows][0]); for (j = 1; j < numCols; j++) { inFile >> matrix[numRows][j]; printf ("%d\t", matrix[numRows][j]); } numRows++; printf ("\n"); } inFile.close ();
You have
C++ Syntax (Toggle Plain Text)
inFile.close ();
at the top before anything is read in, so you want to take that out. I think these lines are going to give you an error:
C++ Syntax (Toggle Plain Text)
printf("\t%d", matrix[i][1],k); printf("\t%d", matrix[i][2],k);
You only have one "%d", but you have two parameters. I'd take the "k" parameter out.
•
•
•
•
Sadly, the c++ compiler i have to use at uni must use those headers, as it isn't a very new program. So I do have to use them
Well, then you didn't read the POST #6 very well, if at all, because it has exactly what you need.
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.
•
•
Join Date: May 2007
Posts: 35
Reputation:
Solved Threads: 2
Dunno what I was thinking at the time.. please forgive me.
'
'
•
•
•
•
Cody: if you want to post code, that is ok, but please
- Use code tags [code=c++] /* code goes here */ [/code]
- Don't use ancient header files with .h extension
- Don't use eof() as you did in line 12 because it doesn't work like that
>> This reads the text in as characters
Why? it could read into integer variables just as easy. By reading in as strings the strings have to be converted into ints before they can be used. That just extra unnecessary work.
Name: Cody Oebel
Place : San Antonio , TX.
Place : San Antonio , TX.
![]() |
Similar Threads
- Help Please, how do i read from text file into array? (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Check for a character input to prevent a program crash
- Next Thread: Problem with my win API
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game 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 number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







