GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Apr-2007, 19:56
Chopo Chopo is offline
New Member
 
Join Date: Apr 2007
Posts: 9
Chopo is on a distinguished road

Importing data from an ASCII file


Hey all,

Suppose i have an ASCII file in which each line contains there values x, y and z separated by a blank space.
Say its a list of vectors for examples.

to import it into a c++ i normally would use the following code:

CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;

int main(){
    
    ifstream input;
    int N; // Number of line of data in ASCII file
    
    double x[N], y[N], z[N];
    
    input.open("fileName.txt");
    
    for(int i=0; i<N; i++){
            input >> x[i];
            input >> y[i];
            input >> z[i];
    }
    
    input.close();
    
    return 0;
}

This is fine as long as the data being imported is numerical, but suppose i have the following scenario: the ASCII file to be imported is a basic database in which each line contains a products reference number, price and product description. The price is a floating point number but the reference number and product descriptions are strings. The matter is complicated further by the fact that the product description can contain blank spaces and can be anything up to (say) 55 characters long.

How would i modify this code to be able to import the data from the database into a c++ program? Seeing as character strings are treated as arrays in themselves would i have to create a class to house multiple product descriptions?

Thanks,

Chopo
Last edited by admin : 04-Apr-2007 at 00:13. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 03-Apr-2007, 20:37
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Inporting data from an ASCII file


Read the entire line into a string. Then look at the string and move to permanent storage
1) the first part of the string (reference number) character by character until you get to the first character that's not a reference number character.
2) Skip the next characters until you hit a price character.
3) Convert to float
4) Skip until you get past the number and to the first character of the description.
5) Move the rest of the string to the description storage.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 04-Apr-2007, 04:53
Chopo Chopo is offline
New Member
 
Join Date: Apr 2007
Posts: 9
Chopo is on a distinguished road

Re: Inporting data from an ASCII file


Thanks WaltP

Ok, here is my modified program according to your, instructions. I have neglected converting the price to float for now. Whilst it compiles there appears to be a bug as if i then create a for loop to output the strings on screen i find that the whole line appearers to be turned into an integer value and only the corresponding character to this number is stored. Could you tell me what i am doing wrong:

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
    
    ifstream input;
    int N=0; // Number of line of data in ASCII file
    string line;
    
    string ref[N], val[N], dscrpt[N];
    
    input.open("fileName.txt");
    
    for(int i=0; i<N; i++){
            
            getline (input,line);
            
            ref[i] = (line, 0, 6);
            val[i] = (line, 7, 6); // I'm asuming there is a 1 character spacing between values
            dscrpt[i] = (line, 14, 20);
            
            cout << ref[i] << " " << val[i] << " " << dscrpt[i] << endl;
    }
    
    input.close();  
    return 0;
}
Last edited by admin II : 04-Apr-2007 at 07:43. Reason: Please surround your CPP code with [CPP] ... [/CPP]
  #4  
Old 04-Apr-2007, 17:19
Chopo Chopo is offline
New Member
 
Join Date: Apr 2007
Posts: 9
Chopo is on a distinguished road

Re: Inporting data from an ASCII file


Also, is there a command to count the number of lines within an ASCII file so that a c++ program can know how large an array it has to create to store all the data within the ASCII file.
  #5  
Old 04-Apr-2007, 18:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Inporting data from an ASCII file


Quote:
Originally Posted by Chopo
Whilst it compiles there appears to be a bug ...
Yep.

What is this line supposed to do?
ref[i] = (line, 0, 6);
I ask because it does nothing. At least nothing you need done. All you did was state the variable and 2 integers. Did you forget a substring or move command?

CPP / C++ / C Code:
int N=0; // Number of line of data in ASCII file
string ref[N], val[N], dscrpt[N];
If N is zero, how many array entries do you suppose ref contains? You need to give a value in the definition so the array can hold the number of refs and vals you need to hold.

Other than that, as long as your data is formatted exactly, this technique will work fine.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #6  
Old 05-Apr-2007, 04:40
Chopo Chopo is offline
New Member
 
Join Date: Apr 2007
Posts: 9
Chopo is on a distinguished road

Re: Inporting data from an ASCII file


With respect to the n=0, i don't think that was the problem, assume its set to however many lines there are in the ASCII file. I left the N=0 there by accident because there originally was a loop in the code that tried to count how many lines there are in the file:
Code:
while (! input.eof() ){ N+=1; }

But this just caused me more problems rather than counting how many lines there were in the document.

as for:
CPP / C++ / C Code:
ref[i] = (line, 0, 6);
You are right that line is wrong. I based it on some code that takes part of an existing string variable and saves it in a new one but it should have the = and only seems to work at the time of defining the variable, so im not sure i could use it in an array. It should reed:
CPP / C++ / C Code:
string ref[i] (line, 0, 6);

So here is my amended code:
CPP / C++ / C Code:
int main(){
    
    ifstream input;
    int N; // Number of line of data in ASCII file
    string line;
    
    string ref[N], val[N], dscrpt[N];
    
    input.open("fileName.txt");
    
    for(int i=0; i<N; i++){
            
            getline (input,line);
            
            ref[i] (line, 0, 6);
            val[i] (line, 7, 6); // I'm asuming there is a 1 character spacing between values
            dscrpt[i] (line, 14, 20);
            
            cout << ref[i] << " " << val[i] << " " << dscrpt[i] << endl;
    }
    
    input.close();  
    return 0;
}

Ofcource it still does not work because as you pointed out ref[i] (line, 0, 6); does not actually take six characters out of string line starting from character 0 nore do the two following lines.

I think i'm suppose to use the get, getword or some related function but do not know how to specify which characters out of the string line should be transfered to variables ref[i], val[i], and dscrpt[i].

Also, is there a command to count the number of lines within an ASCII file so that a c++ program can know how large an array it has to create to store all the data within the ASCII file.
  #7  
Old 05-Apr-2007, 11:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Inporting data from an ASCII file


Quote:
Originally Posted by Chopo
With respect to the n=0, i don't think that was the problem, assume its set to however many lines there are in the ASCII file. I left the N=0 there by accident because there originally was a loop in the code that tried to count how many lines there are in the file:
Code:
while (! input.eof() ){ N+=1; }

But this just caused me more problems rather than counting how many lines there were in the document.
It doesn't matter what you want to do with N later, if you set N to zero and use it to define an array, you do not get enough array entries to store anything. Period.

Think about it this way. You want a box that can hold balls. N is used to define how many balls fit in the box. You set N to zero, grab a box that can hold N balls. How big is that box? You need to define your string to hold the proper number of values. You can't base it on N.

Quote:
Originally Posted by Chopo
as for:
CPP / C++ / C Code:
ref[i] = (line, 0, 6);
You are right that line is wrong. I based it on some code that takes part of an existing string variable and saves it in a new one but it should have the = and only seems to work at the time of defining the variable, so im not sure i could use it in an array. It should reed:
CPP / C++ / C Code:
string ref[i] (line, 0, 6);
Also does nothing useful. You need to look into the methods of the string object, like replace(), substr(), find(), and so on. There are a lot of things you can do with a string, you have to examine these methods.


Quote:
Originally Posted by Chopo
So here is my amended code:
CPP / C++ / C Code:
int main(){
    
    ifstream input;
    int N; // Number of line of data in ASCII file
    string line;
    
    string ref[N], val[N], dscrpt[N];  //** N has no valid value, does not 
                                       //** define your array properly
    
    input.open("fileName.txt");
    
    for(int i=0; i<N; i++){    //** N has no value, probably a compiler warning
            
            getline (input,line);
            
            ref[i] (line, 0, 6);  //** Does nothing.  Probably a compiler error
            val[i] (line, 7, 6); // I'm asuming there is a 1 character spacing between values
            dscrpt[i] (line, 14, 20);
            
            cout << ref[i] << " " << val[i] << " " << dscrpt[i] << endl;
    }
    
    input.close();  
    return 0;
}

Ofcource it still does not work because as you pointed out ref[i] (line, 0, 6); does not actually take six characters out of string line starting from character 0 nore do the two following lines.

I think i'm suppose to use the get, getword or some related function but do not know how to specify which characters out of the string line should be transfered to variables ref[i], val[i], and dscrpt[i].
See above

Quote:
Originally Posted by Chopo
Also, is there a command to count the number of lines within an ASCII file so that a c++ program can know how large an array it has to create to store all the data within the ASCII file.
No, you have to, at least at this point, assume a maximum size and just use that. You already know your data, just make the arrays larger than the number of lines you have for now. You're already assuming the format, assume the max size, too. And your while loop is better for this since the program doesn't know the number of lines it's going to read.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #8  
Old 05-Apr-2007, 12:48
darelf darelf is offline
Junior Member
 
Join Date: Feb 2007
Posts: 68
darelf will become famous soon enough

Re: Inporting data from an ASCII file


CPP / C++ / C Code:
#include <vector>
#include <fstream>

void tokenize( const string & str, vector<string> & tokens, const string & delim ) {
  string::size_type lastPos = str.find_first_not_of( delim, 0 );
  string::size_type pos      = str.find_first_of( delim, lastPos );

  while( string::npos != pos || string::npos != lastPos ) {
    tokens.push_back( str.substr( lastPos, pos - lastPos ) );
    lastPos = str.find_first_not_of( delim, pos );
    pos = str.find_first_of( delim, lastPos );
  }
}

ifstream f( "file_name" );
string inputString;
getline( f, inputString );

vector<string> tokens;
tokenize( inputString, tokens, " " );

At that point, tokens[0] contains the first item, tokens[1] the second, and tokens[3] the third. Move/convert them from there.

As for the loop, why would you not read until the end of the file?
  #9  
Old 05-Apr-2007, 16:03
Chopo Chopo is offline
New Member
 
Join Date: Apr 2007
Posts: 9
Chopo is on a distinguished road

Re: Inporting data from an ASCII file


Quote:
Originally Posted by darelf

As for the loop, why would you not read until the end of the file?

I wanted to count the number of lines in the file before running through it in order to know how large the arrays have to be in order to store all the entries in the ASCII file.

Thanks for the code by the way
  #10  
Old 05-Apr-2007, 16:32
Chopo Chopo is offline
New Member
 
Join Date: Apr 2007
Posts: 9
Chopo is on a distinguished road

Re: Inporting data from an ASCII file


WaltP, you are giving to much importance to the N bug. I know that i need to set a value for N in order to get the loop to work, i was just trying to keep the example generic, the code i am actually working on is effectively identical but has more arrays and some other stuff which was not relevan to my question. Seeing as this has caused confusion i will assume N=85 from now on.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.