Reading From Formatted text file C++

How to read from a text file and store for in a Binary Tree

Page 1 of 1

4 Replies - 4562 Views - Last Post: 16 October 2009 - 05:47 PM Rate Topic: -----

#1 intmain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-October 09

Reading From Formatted text file C++

Posted 15 October 2009 - 11:42 PM

Hello,

I've been working a project that reads data from a text file that is formatted like this:

8ItemName ItemID Price Quantity
2ItemName ItemID Price Quantity
10ItemName ItemID Price Quantity
...
....
......

NOTE: There is no spacing between the beginning numbers(Customer ID) and the item Name.

This will keep going for about 200 records. As of now all I want to do is read in those records and store it in a binary file, as well as inserting the ItemID(the only unique number) into my AVL tree.

Here is my code:

// read a file into memory
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main () 
{
  int length = 70; //length of one line or record
  char * buffer;
  string temp;
  int n;
  struct record
	{
		int custID;
		int partQuant;
		string partDesc;
		string partNum;
		double partPrice;
	};
  
  record *data;
  
	 
  cout << "Please enter a record line: ";
  cin >> n;
  
  ifstream is;
  is.open ("sales.dat", ios::in | ios::binary ); 
  
  if( is.fail() )	   //check if file exists
  {
	  cout << "Cannot find the file 'sales.dat'." << endl;
	  system("pause");
	  return 0;
  }
  
  while(!(is.eof()))
  {		   
	 is.read (buffer,length);
	 //cout.write (buffer, length);
  }
  
  int index = 0;
  while (index <= n)
  {
	  istringstream input(buffer);
	  input.get(data->partDesc, 40);
				
	  input >> data->partNum >> data->partPrice >> data->partQuant;
	  input.ignore(128,'\n');
			
	  cout << data->partDesc <<"$"<< data->partNum <<"$"<< data->partPrice <<"$"<< data->partQuant <<"$"<< endl;
	  index++;
  }
  
  is.close();

  delete[] buffer;
  system("pause");
  return 0;
}


This code compiles, but then terminates when the CMD window is opened. If anyone could be so kind as to point out the error or a better solution to my particular problem I would be very grateful.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading From Formatted text file C++

#2 Guest_c.user*


Reputation:

Re: Reading From Formatted text file C++

Posted 15 October 2009 - 11:59 PM

where is the allocation of memory for the buffer ?
Was This Post Helpful? 0

#3 intmain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-October 09

Re: Reading From Formatted text file C++

Posted 16 October 2009 - 03:52 PM

View Postc.user, on 15 Oct, 2009 - 10:59 PM, said:

where is the allocation of memory for the buffer ?


I forgot about that, I've entered in this code just before the while (!(is.eof())) loop:

  buffer = new char [length];


This lets me into the program, but now I get this error:

52 no matching function for call to `std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >::get(std::string&, int)'
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Reading From Formatted text file C++

Posted 16 October 2009 - 05:11 PM

In response to the error:

get is a member function of the stringstream class. Take a look at [url=http://www.cplusplus.com/reference/iostream/istream/get/] the function arguments for the overloaded variations of this function[[url]. Now point out to me which variation takes in a string as the first parameter.

Also, another 2 questions for you.

What do you believe eof() tells you? Why do you think this?
Was This Post Helpful? 0
  • +
  • -

#5 intmain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-October 09

Re: Reading From Formatted text file C++

Posted 16 October 2009 - 05:47 PM

View PostOler1s, on 16 Oct, 2009 - 04:11 PM, said:

In response to the error:

get is a member function of the stringstream class. Take a look at [url=http://www.cplusplus.com/reference/iostream/istream/get/] the function arguments for the overloaded variations of this function[[url]. Now point out to me which variation takes in a string as the first parameter.

Also, another 2 questions for you.

What do you believe eof() tells you? Why do you think this?


Thanks for the reply, on the link you provided I found the following, it seems I can't store the data in a string, but a c-string.

istream& get (char* s, streamsize n );
Extracts characters from the stream and stores them as a c-string into the array beginning at s.

EOF tells me that the program has reached the end of the file, I wanted it to keep reading in the lines until the eof, that's why I put the read in a while(!eof) statement.

I changed the partDesc variable in my struct to char and now, the error is:\
52 invalid conversion from `char' to `char*'
52 initializing argument 1 of `std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'

This is the code so far:

// read a file into memory
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main () 
{
  int length = 70; //length of one line or record
  char * buffer;
  string temp;
  int n;
  struct record
	{
		int custID;
		int partQuant;
		char partDesc;
		int partNum;
		double partPrice;
	};
  
  record *data;
  
	 
  cout << "Please enter a record line: ";
  cin >> n;
  
  ifstream is;
  is.open ("sales.dat", ios::in | ios::binary ); 
  
  if( is.fail() )	   //check if file exists
  {
	  cout << "Cannot find the file 'sales.dat'." << endl;
	  system("pause");
	  return 0;
  }
  
  buffer = new char [length];

  while(!(is.eof()))
  {		   
	 is.read (buffer,length);
	 //cout.write (buffer, length);
  }
  
  int index = 0;
  while (index <= n)
  {
	  istringstream input(buffer);
	  input.get(data->partDesc, 40);
				
	  input >> data->partNum >> data->partPrice >> data->partQuant;
	  input.ignore(128,'\n');
			
	  cout << data->partDesc <<"$"<< data->partNum <<"$"<< data->partPrice <<"$"<< data->partQuant <<"$"<< endl;
	  index++;
  }
  
  is.close();

  delete[] buffer;
  system("pause");
  return 0;
}




EDIT-------------------------------

Ok so I experimented with the struct and changed the char partDesc to a char *partDesc. This then allows me to run the program and the syntax is error free, but as soon as a I enter a line number the program terminates with an error "The memory could not be read", along with standard Windows cryptic error hex codes.

Thanks.

This post has been edited by intmain: 16 October 2009 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1