| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Reading a formated text file into a two dimensional array
Hi,
How do I read a file into a two dimensional array? I have a space formated file that looks like this: 950 912828C4U ESMITH 94000.46 20040702 11 912828C50 ESMITH 1100.00 20040702 2200 912828RC5 ESMITH 220875.00 20040702 What I want to be able to do is read each each line into an array such as this: string [100][5] Once it is in the array I will format the output so that it looks like this: OPE,950,912828C4U,ESMITH,94000.46,20040702,0,0,0, I've been able to format the output to file using the following code (in this code the array values are predefined) : #include <iostream> #include <string> #include <fstream> using namespace std; int main() { string test [2] [5] = { {"820", "912828CYO", "FSMITH", "20040412", "8100.00"}, {"900", "912828CYO", "FSMITH", "20040412", "9000.00"}, }; ofstream file("f:/files/out.txt", ios: ut);for (int row = 0; row < 2; row++) { file << "ope,"; for (int col = 0; col < 5; col++) { file << test[row][col] <<","; } file << "0,0,0" << endl; } return 0; } Basicly what I am attempting to do is read in a space delimeted file and output a comma delimeted file. Can anyone give me any sugestions? |
|
#2
|
|||
|
|||
|
Personally I would just read through the file one character at a time and output it to a new file, replacing it with a comma if a space is found.
-KM- |
|
#3
|
|||
|
|||
|
Thanks for the reply. I've decided to drop the array completely and read the file in and out using the fcode below .
while(!fileIn.eof( )) { fileIn >> example >> test ; fileOut << example << test; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Reading a formated text file into a two dimensional array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|