473,503 Members | 4,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading data from text file WITHOUT using standard template library

30 New Member
Currently I need to read data from a file, such like this

Measurements for Lansing, Michigan during April, 2000
63 32 0.00
54 43 0.10
59 39 0.00
46 24 0.00
52 20 0.00
54 30 0.00

The first line is obviously disregarded, since all we want are the numbers.

We are not allowed to use anything from standard template library, vectors to be more precise

I had something like this for now, though I'm not sure if it cuts the first line out or not...I'm also cutting main() off the post, I know it works, its the I/O I don't get.

Expand|Select|Wrap|Line Numbers
  1. void read( ifstream& In, ReadData List[], int Size, int& Num )
  2. {
  3.   ReadData Temp;
  4.  
  5.   Num = 1;
  6.  
  7.   for (;;)
  8.   {
  9.     In >> Temp.Min >> Temp.Max >> Temp.Precipt;
  10.  
  11.     if (In.fail() || Num >= Size) break;
  12.  
  13.     List[Num] = Temp;
  14.  
  15.     Num++;
  16.   }
  17. }
  18.  
  19.  
  20. void process( const ReadData List[], int Num )
  21. {
  22.   int I;
  23.   //***************************
  24.   // Display the column headers
  25.   //***************************
  26.  
  27.   cout << "\n";
  28.   cout << "Minimum Value      Maximum Value     Precipitation\n";
  29.   cout << "-------------      -------------     -------------\n";
  30.  
  31.   for(I=0;I<Num;I++)
  32.   {
  33.     cout << resetiosflags( ios::right ) << setiosflags( ios::left );
  34.     cout << setw(20) << List[i].Min << "          "<< List[i].Max << "          "<< List[i].Precipt <<endl;
  35.   }
  36. }
  37.  
as for declaring the struct, its
Expand|Select|Wrap|Line Numbers
  1. struct ReadData
  2. {
  3.   int Min,Max;
  4.   double Precipt;
  5. };
  6.  
Currently the output yeilds
0 0 (some reference address)

...and thats it

I know i'm somewhere close, but I'm drawing a blank. I set Num = 1 in hopes of skipping the first set in the data, which would be the string of words.

Your Help is Greatly Appreciated.
Feb 10 '08 #1
5 2090
weaknessforcats
9,208 Recognized Expert Moderator Expert
I hate to break this to you but you are using ifstream and the >> operator and these are part of the STL.

You have to throw your C++ book in the garbage and a copy of the ANI C Programming Language from , oh say, 1988 and then learn how it was done twenty years ago.

That is you need to use the C FILE*.

Is this some kind oddball class problem??? If so, your course needs severe updating.
Feb 10 '08 #2
Myxamatosis
30 New Member
Hmm. Considering That i'd have to use a time machine to write this program, perhaps our professor just wants us to read and process data without using vector streams.

...at least i hope.

So without using vector streams, am i close?
Feb 10 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Yes, you are close.

However, if you use an array you code:
Expand|Select|Wrap|Line Numbers
  1. List[Num] = Temp;
  2.  
and if you use a vector=<ReadData> you code:
Expand|Select|Wrap|Line Numbers
  1. List[Num] = Temp;
  2.  
In the first case you have to worry about max array size, overruning the array bounds, etc.

With a vector, you don't.

That is, assume the array and the vector have 10 elements and for some reason Num is 20. In the first case, no error occurs. Temp just get written as element 21, which is outside the array and this stomps on something else. This means the buggy code (using arrays) corrupts memory and just keeps going.

With a vector, you crash right now tryinf to go outside the vector.

Arrays are built-in containers from C and have no real place in C++ other than to compile relic C code. The less you know about them, the better. However, an old teaching paradigm was to learn C (like arrays) and then learn C++ (like vectors). This approach has been debunked many times as atavistic.

Plus a vector is an array. It just has the code you would need to write to use the array. So, every time you use an array you are re-inventing the wheel and duplicating already written code.
Feb 10 '08 #4
Myxamatosis
30 New Member
alright, upon reviewing the assignment, it seems we're supposed to use something along the lines of getline() from the string class library.

Now granted if this was python, I could do this no problem, but I'm a bit rusty at my c++, it's been 3 years since I've used it.

So Is there a way to salvage my code by using getline?
Feb 10 '08 #5
Myxamatosis
30 New Member
I'm on the right track with the sample code I had originally posted...we only needed to use getline() to possibly skip the first line of data, which is the string.

However, I'm still not sure why I'm getting the output I have, which is 0 0 and some reference address.

I need to use arrays, which I understand has the potential to overload itself...but I don't know where in my code I went wrong for reading and outputting the data.

If you need the main() part to test it yourself, i'll post if needed.

Thanks Much
Feb 19 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
14638
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
1
7032
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
2
10658
by: Roland Hall | last post by:
I have two(2) issues. I'm experiencing a little difficulty and having to resort to a work around. I already found one bug, although stated the bug was only in ODBC, which I'm not using. It...
14
5811
by: Roland Hall | last post by:
I have two(2) issues. I'm experiencing a little difficulty and having to resort to a work around. I already found one bug, although stated the bug was only in ODBC, which I'm not using. It...
3
2746
by: Roland Hall | last post by:
Three times the charm? Sorry for the repost. Trying to get my account right. I have two(2) issues. I'm experiencing a little difficulty and having to resort to a work around. I already...
6
3747
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
8
7435
by: Vijay | last post by:
Hi. I have a binary file which is on a CD it has filesize around 300 MB. Its not a text file. I want to read it and copy its content to a new file on hard disk. I dont want to use filecopy. I...
50
4879
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
6
6317
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.