| | |
inputing a text file into an array
Thread Solved
![]() |
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Solved Threads: 0
Hi,
I'm trying to create a 2d array that will fill itself up with numbers i have in a text file.
My text file looks like this:
1 5 4
2 6 3
3 9 4
4 1 8
5 2 2
So, I want to create an array with 5 rows and 3 columns that will print this exact output to the screen.
Could anybody help me with the code please?
Thank you
I'm trying to create a 2d array that will fill itself up with numbers i have in a text file.
My text file looks like this:
1 5 4
2 6 3
3 9 4
4 1 8
5 2 2
So, I want to create an array with 5 rows and 3 columns that will print this exact output to the screen.
Could anybody help me with the code please?
Thank you
>> Could anybody help me with the code please?
Not unless you show some work.
But it's not that hard. There are two problems to overcome -- 1) filling the array 2) reading values from a file. Once you have those two done there's really no problem. Show attempts on both.
Not unless you show some work.
But it's not that hard. There are two problems to overcome -- 1) filling the array 2) reading values from a file. Once you have those two done there's really no problem. Show attempts on both.
•
•
Join Date: May 2007
Posts: 29
Reputation:
Solved Threads: 2
c++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <fstream.h> int main() { /* open the file to read from in this case being your text file*/ char chars[50]; char newline = '\n'; // for the get line function's 3rd argument .. acts as a terminating delimeter. ifstream in("c:\\documents and settings\\your profile name\\desktop\\theNameOftheTXTfile.txt"); while(! in.eof()) { in.getline(chars,50,newline); cout<<chars<< endl; }; system("pause"); // I use DEV as im a hobby //programmer. return 0; };
NOTE: This reads the text in as characters, so although they are numbers in the note pad, they will be read in as characters. **some people like to write stuff to text files, and read from text files without having to use the isstream class if your doing most of your work on a windows platform.
Last edited by Ancient Dragon; Jan 26th, 2008 at 9:33 pm. Reason: add code tags
Name: Cody Oebel
Place : San Antonio , TX.
Place : San Antonio , TX.
Cody: if you want to post code, that is ok, but please
>> 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.
- 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.
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
Ok, sorry for not showing you what i've got.
I'm looking for something really simple, all I want to do is fill an array/matrix with my infile!!
In my example, there were only a few rows of information, but this needs to be able to be replaced with something, say, of 100 row of information. Thus, the program needs to be quite general.
I thought maybe zero the array first?
At the moment I have this code for the array...
k = 0;
for (i=1; i<101; i++)
{
for (j=1; j<3; j++)
{
matrix[i][0] = i;
matrix[i][1] = k;
matrix[i][2] = k; }
printf("%d", matrix[i][0]);
printf("\t%d", matrix[i][1],k);
printf("\t%d", matrix[i][2],k);
printf("\n");}
}
And as for uploading the input file from notepad, i believe i should use...
inFile.open("input.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
Am I on the right tracks? Oh, and that code already posted din't work at all
I'm looking for something really simple, all I want to do is fill an array/matrix with my infile!!
In my example, there were only a few rows of information, but this needs to be able to be replaced with something, say, of 100 row of information. Thus, the program needs to be quite general.
I thought maybe zero the array first?
At the moment I have this code for the array...
k = 0;
for (i=1; i<101; i++)
{
for (j=1; j<3; j++)
{
matrix[i][0] = i;
matrix[i][1] = k;
matrix[i][2] = k; }
printf("%d", matrix[i][0]);
printf("\t%d", matrix[i][1],k);
printf("\t%d", matrix[i][2],k);
printf("\n");}
}
And as for uploading the input file from notepad, i believe i should use...
inFile.open("input.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
Am I on the right tracks? Oh, and that code already posted din't work at all
•
•
Join Date: Jan 2008
Posts: 3,756
Reputation:
Solved Threads: 491
You don't need to zero out the array. You already know the size (5 x 3), so declare it that size:
i would just read it in like this:
C++ Syntax (Toggle Plain Text)
int matrix[5][3];
i would just read it in like this:
C++ Syntax (Toggle Plain Text)
for (int i = 0; i < 5; i++) { for (j = 0; j < 3; j++) { inFile >> matrix[i][j]; // display matrix[i][j] } }
•
•
•
•
Originally Posted by VernonDozier
You don't need to zero out the array. You already know the size (5 x 3), so declare it that size:
•
•
•
•
Originally Posted by andyg55
In my example, there were only a few rows of information, but this needs to be able to be replaced with something, say, of 100 row of information. Thus, the program needs to be quite general.
*Voted best profile in the world*
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Solved Threads: 0
So here is the basic program I have. All this does is read a .txt file and produce a 100x3 array that is zero'd. What alterations/additions do I make to the code to import the .txt file into the array? Thanks in advance!!
c++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <fstream.h> #include <stdlib.h> int k,i,j; int matrix[101][5]; main () { ifstream inFile; inFile.open("input.txt"); if (!inFile) { cout << "Unable to open file"; exit(1); // terminate with error } inFile.close(); k = 0; for (i=1; i<101; i++) { for (j=1; j<3; j++) { matrix[i][0] = k; matrix[i][1] = k; matrix[i][2] = k; } printf("%d", matrix[i][0]); printf("\t%d", matrix[i][1],k); printf("\t%d", matrix[i][2],k); printf("\n");} }
Last edited by Ancient Dragon; Jan 27th, 2008 at 6:01 pm. Reason: add code tags
![]() |
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: Looping Problem and some questions
- Next Thread: Setting Array to Zero with Constructor
| Thread Tools | Search this Thread |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix matrix3d memory multiple news node output parameter pointer problem program programming project python random read recursion reference rpg std::coutwstring string strings temperature template test text text-file tree url variable vector video visualization win32 windows winsock word wordfrequency wxwidgets







