C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 2nd, 2004, 05:26 PM
ddeile ddeile is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 ddeile User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old July 3rd, 2004, 01:50 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
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-

Reply With Quote
  #3  
Old July 4th, 2004, 03:47 PM
ddeile ddeile is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 ddeile User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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;

}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Reading a formated text file into a two dimensional array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT