#1
  1. Bad Andy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    OH
    Posts
    275
    Rep Power
    12

    read in file and graph it


    Hey i'm back. Did eveyone miss me? Well, this time the problem is a bit more specific. Here is my task: I must read in specifically formatted txt file, take that info and created an X-Y plot with it. The limited experience I have had w/ ifstream etc. is not proving to be enough. The files path is stored in the AnsiString FileName. Now, I'm pretty sure ifstream recognizes line breaks, but what about spaces? For instance, what if I wanted to read in a txt file that looked like:

    # # # #
    # # # #

    do numbers also have the '/0' end character?

    I have included an excel file which has all the info that I need to take and put into a graph. One tab is one graph, so a single tab is all that would go into the txt file. So, take a look, and after a couple people have seen it to know what I'm talking about, I will post some questions about specific syntaxes. Thanks.
    Attached Files
  2. #2
  3. Bad Andy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    OH
    Posts
    275
    Rep Power
    12
    ah well, guess i'll get a fresh start on Mon.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Forum was down at end of lunch, then I had meetings and other business.

    I never really went in for iostreams, so I had to write a sample program to test it out. To create the text file, I copied the first 15 rows of data from your Excel file to a text file:

    Code:
    #include <fstream.h>
    
    int main(void)
    {
        double f1, f2, f3, f4;
        ifstream inf;
        inf.open("xdata.txt");
    
        // read in the first four values, then echo them out
        inf >> f1 >> f2 >> f3 >> f4;
        cout << f1 << " " << f2 << " " << f3 << " " << f4 << endl;
    
        // read in the next four values, then echo them out
        inf >> f1 >> f2 >> f3 >> f4;
        cout << f1 << " " << f2 << " " << f3 << " " << f4 << endl;
    
        inf.close();
        return 0;
    }
    
    C:\dcw\PROJECTS>head xdata.txt
    0.03    44.00   47.29   8.71
    0.01    44.06   47.38   8.57
    0.04    44.07   47.15   8.78
    0.01    43.92   47.45   8.64
    0.00    44.20   47.21   8.59
    0.06    44.01   47.22   8.77
    0.00    44.11   47.29   8.60
    0.03    44.34   47.15   8.51
    0.00    44.30   47.06   8.64
    0.03    44.59   46.84   8.56
    
    Program Output:
    0.03 44 47.29 8.71
    0.01 44.06 47.38 8.57
    So then, yes, ifstream treats spaces as delimiters between values. Plus, I believe that it automatically goes to the next line when it needs to. So then really, I'm just having it read four values at a time and it will do so no matter how the lines are set up. Though you should keep it four to a line so that the data file will be more human-readable.

    You mentioned that the filename is in an AnsiString. If ifstream::open doesn't like that, you should be able to use the c_str method to provide it a char* :
    Code:
    AnsiString fname;
    
    inf.open(fname.c_str);
    As to your question, "do numbers also have the '/0' end character?":
    First, you mean '\0', the null terminator.
    Second, the null terminator only applies to string variables. The numbers in your data file are actually strings.
    Third, ifstream::>> performs the conversion from string to numeric (double in my test), so you don't have to worry about it.

    BTW, when in doubt about something, write a short test program.
    Last edited by dwise1_aol; June 13th, 2003 at 06:27 PM.
  6. #4
  7. Bad Andy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    OH
    Posts
    275
    Rep Power
    12
    Great. That helps alot. Thank you very much for taking the time to do that.
    Last edited by AGibel; June 17th, 2003 at 09:52 AM.

IMN logo majestic logo threadwatch logo seochat tools logo