4 Replies - 2137 Views - Last Post: 10 February 2009 - 01:25 PM Rate Topic: -----

#1 Doc78610  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-February 09

Reading spaces from a file

Post icon  Posted 09 February 2009 - 05:17 PM

If i was trying to read a line of info from a file.txt, how would i read the entire line and include the spaces?

for examples, i need to read a line that is like and then another: abdefg F50.50
zxywv A30.30

When the program stores the input, it only does so up until the space. How can i get it to read then ENTIRE line so i can manipulate the whole thing as a string?

Is This A Good Question/Topic? 0
  • +

Replies To: Reading spaces from a file

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Reading spaces from a file

Posted 09 February 2009 - 05:49 PM

Im taking a guess your using " >> " to do this right?
Well, assuming I'm right (and I may infact be completely wrong) >> stops reading at spaces
so, I would say use either
whatever_file.getline(var_name_to_store_text, '\n'); 
//  '\n' is a delimiter where getline stops reading, in 
// this case, the newline character (enter) so
// only 1 line of text is read

//or put it in a for() loop

for(i=0; i<=length_of_file; i++)
	whatever_file.get(var_to_store_it[i]);
// This would read the entire file



Just as a warning, I didn't actually check to see if this would work and it might not work at all.

However here and here might be useful to you.
Was This Post Helpful? 0
  • +
  • -

#3 Doc78610  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-February 09

Re: Reading spaces from a file

Posted 09 February 2009 - 06:11 PM

I tried the coding you suggested, but it doesnt seem to work. Am i doing something wrong or missing something?
Any other suggestions?
I commented NEW SUGGESTED LINES next to the code i tried. Thanks



/*
This program will convert fahrenheit into celcius and organize the dates listed 
for each segment of data recorded on a day into a easily readable format.   */

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
using std::ifstream;
using std::ofstream;
using std::ios;

//void Parsing (&string, &string);
//double TemperatureConv(double);
void PrintFunction();

int main()
{
   ifstream biodata_unsorted;	  //created input from the file read
   ofstream biodata_sorted;		//file will be written to here
   
   biodata_unsorted.open ("biodata.txt", ios_base::in);
   biodata_sorted.open ("biodata_fixed.txt", ios_base:: out);
   
   if (biodata_unsorted.fail())
	  {
	  cout<<"ERROR OPENING FILE.";
	  return EXIT_FAILURE;
	  }
   

   string biodata_unfiltered;				//created variable to store read data
   int biodata_lines;			 //created variable to store read data
   
   string biodata_filtered;				  //created variable to store FIXED data
   const int SIZE = 30;					  //Max size of array set
   biodata_unfiltered[SIZE];
   
   //cout << biodata_unfiltered[0];
   
   biodata_unsorted >> biodata_lines;
   int i = biodata_lines;
   char biodata_array[SIZE]; 
   int counter = 0;				//array named and initialized
  
   cout << biodata_lines << " is biodata unlines and the lines to read" 
		<< endl << endl;
  
   //biodata_unsorted >> biodata_unfiltered;
   biodata_unsorted.getline(biodata_unfiltered, '\n');				 //NEW SUGGESTED LINES
   for (int i = 0; i < biodata_unfiltered.length(); i++)
	   {
			cout << biodata_unfiltered[i] << " of element " << i;
			cout << endl;
			}
					   
   
   while (!biodata_unsorted.eof() && counter < biodata_lines)
   {
		 //This is all parsing the information extracted from the file
		 /*
		 The format of the data as read from the file was this:
			 2
			 200707211245 F70.5
			 200708220812 C19.97
		 */
		 
		 //biodata_unsorted >> biodata_unfiltered;		  //storing unsorted into unfiltered
		 
		 cout << biodata_unfiltered << " was unfiltered... but now its not." 
			  << endl << endl;
		 
		 cout << "This is filtered data organized correctly." << endl << endl;
		 biodata_filtered =  biodata_unfiltered.substr(14, 4);
		 biodata_filtered += " ";
		 
		 biodata_filtered += biodata_unfiltered.substr(4,2);
		 biodata_filtered += '/';
		 biodata_filtered += biodata_unfiltered.substr(6,2);
		 biodata_filtered += '/';
		 biodata_filtered += biodata_unfiltered.substr(0,4);
		 biodata_filtered += " at ";
		 biodata_filtered += biodata_unfiltered.substr(8,4);
		 
		 
		
		 cout << " recorded on " << biodata_filtered << endl << endl;
		 biodata_sorted << biodata_filtered << endl;
		 //biodata_unsorted >> biodata_unfiltered;
		 
		 //biodata_filtered = biodata_unfiltered.substr(4,2);
		 //biodata_filtered += '/';
		 biodata_filtered += biodata_unfiltered.substr(6,2);
		 biodata_filtered += '/';
		 biodata_filtered += biodata_unfiltered.substr(0,4);
		 biodata_filtered += " at ";
		 biodata_filtered += biodata_unfiltered.substr(8,4);
		 counter ++;
		 system ("pause");
   }
  
  
   PrintFunction();					//Print function call
  //biodata_unsorted.close();
  //biodata_sorted.close();
  system ("pause");
  return 0;   
}


void PrintFunction()
{
	 cout << "Print function testing successful. " << endl;
}


Was This Post Helpful? 0
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Reading spaces from a file

Posted 10 February 2009 - 01:01 PM

EDIT: sorry, skipped over the right part of the code


Hmm, I can't see what your doing wrong, but its probably strikingly obvious and I'm reading right over it.

check out the documentation for getline here and see if the answers your problem

This post has been edited by erik.price: 10 February 2009 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: Reading spaces from a file

Posted 10 February 2009 - 01:25 PM

// try code like this to read a whole file ...
// line by line ... each line into a C++ string called 'line'

// ...

ifstream fin( "yourFileToRead.txt" );
if( ! fin )
{
   cout << "Problem reading file ... Press 'Enter' to exit ... " << flush;
   cin.get();
   return -1;
}

string line;
while( getline(fin, line) )
{
   cout << line << endl;
}
fin.close();

// ...



Shalom,
David

This post has been edited by David W: 10 February 2009 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1