C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 13th, 2003, 12:20 PM
AGibel's Avatar
AGibel AGibel is offline
Bad Andy
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: OH
Posts: 275 AGibel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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
File Type: zip combined data.zip (68.6 KB, 320 views)

Reply With Quote
  #2  
Old June 13th, 2003, 02:37 PM
AGibel's Avatar
AGibel AGibel is offline
Bad Andy
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: OH
Posts: 275 AGibel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
ah well, guess i'll get a fresh start on Mon.

Reply With Quote
  #3  
Old June 13th, 2003, 06:24 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,833 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 3 h 47 m 7 sec
Reputation Power: 446
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.

Reply With Quote
  #4  
Old June 16th, 2003, 08:14 AM
AGibel's Avatar
AGibel AGibel is offline
Bad Andy
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: OH
Posts: 275 AGibel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > read in file and graph it


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 5 hosted by Hostway