473,569 Members | 2,836 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 2097
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=<ReadDat a> 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
14643
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
7038
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 file... Thank you to all of you who can help me with this one...
2
10671
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 appears to be in the OLEDB driver also. My connection was: conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";" & "Extended...
14
5817
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 appears to be in the OLEDB driver also. My connection was: conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";" & "Extended...
3
2747
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 found one bug, although stated the bug was only in ODBC, which I'm not using. It appears to be in the OLEDB driver also. My connection was:
6
3758
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 trying to read a flatfile. In COBOL, my simple file layout and READ statement would look like below. Question: what is the standard, simple...
8
7442
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 want to open it and read its content and copy to the file which is created on the harddisk. How can I do this. fgets will require specifica no...
50
4898
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 -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
6
6329
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 browsing through this group and other sources on the web, it seems that there are many ways to do it. Some suggest that simply fseek'ing to 8K bytes...
0
7695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.