GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Sep-2006, 07:38
dadio22 dadio22 is offline
Junior Member
 
Join Date: Sep 2006
Location: Breda, The Netherlands
Posts: 49
dadio22 is on a distinguished road

opening, reading adding writing to a text file


heey people,

I'm a newbie to c++ and got a problem with it. I need to open a text file need to know how big it is no problem their haha then read it.

The problem is that i need to do an equation if the character read is smaller then hex 20 then this character--> ' is placed their and the character is written to the textfile.

I found after some looking how to read a text file character by character but how can i do that equation that's my problem. I will put the code i think i'll use to read the text file

CPP / C++ / C Code:

#include <iostream>

using std::cout;

#include <fstream>

using std::ios;
using std::ifstream;

int main()
{
  ifstream fin;                   // Initialise filestream object.
  char c;
  
  fin.open("D:/file conversion/test.txt", ios::in);    // Open an input filestream.
  
  // Check if file opened.
  // fin.fail() returns 1 if there is a fail in the filestream.
  if(fin.fail())
  {
    cout << "Error: Unable to open test.c.\n";
    exit(1);
  }
  
  fin.get(c);                    // Get first character for kicks.
   
  // While the stream hasn't failed or reached the end of file, read and display.
  while(!fin.fail() && !fin.eof())
  {
    cout << c;                 // Display character.
    fin.get(c);                 // Get the next character from the stream.
  }
  
  fin.close();                  // Always close the stream once done.

  return(0);


ps: I hope that this question is done according to rules if not let me know
  #2  
Old 18-Sep-2006, 09:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,303
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: opening, reading adding writing to a text file


Quote:
Originally Posted by dadio22
read it.

The problem is that i need to do an equation if the character read is smaller then hex 20 then this character--> ' is placed

You could try something like:
CPP / C++ / C Code:
        if (c < 0x20) {
            cout << '\''; // special character: a single quote 
        }
        else {
            cout << c;              // Display character.
        }

Regards,

Dave
  #3  
Old 18-Sep-2006, 09:48
dadio22 dadio22 is offline
Junior Member
 
Join Date: Sep 2006
Location: Breda, The Netherlands
Posts: 49
dadio22 is on a distinguished road

Re: opening, reading adding writing to a text file


Hmmm would that work

A character or multiple ones that is higher then 0x20 needs to be placed between quotes so if the first character is read and it's higher then 0x20 you first need to place a quote and then the character you read that's in the beginning at the end you also need to place a quote. I've modified my program a bit maybe now you'll better see what I want to do.
CPP / C++ / C Code:
#include <iostream>

using std::cout;

#include <fstream>

using std::ios;
using std::ifstream;

int main()
{
  ifstream fin;                   // Initialise filestream object.
  char c;
  bool quoteflag;
  int index;
quoteflag = false;

  fin.open("D:/file conversion/test.txt", ios::in);    // Open an input filestream.
  
  // Check if file opened.
  // fin.fail() returns 1 if there is a fail in the filestream.
  if(fin.fail())
  {
    cout << "Error: Unable to open test.c.\n";
    exit(1);
  }
  
  fin.get(c);                    // Get first character for kicks.
   
  // While the stream hasn't failed or reached the end of file, read and display.
while(!fin.fail() && !fin.eof())
  {
    if (c< 0x20)
	{
		if (quoteflag = true)
		{
			quoteflag = false;
			cout<< '\'';
	//		fin.get(c);
	}
		else
		cout << c;                 
		fin.get(c);
		

//	else	
		if (quoteflag = false)
		{
			quoteflag = true;
			cout<< '\'';
	//		fin.get(c);
	}
		else
		cout << c;                 
		fin.get(c);
	
	}						
  }
  
  fin.close();                  // Always close the stream once done.

  return(0);

}
  #4  
Old 18-Sep-2006, 14:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,303
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: opening, reading adding writing to a text file


Quote:
Originally Posted by dadio22
A character or multiple ones that is higher then 0x20 needs to be placed between quotes

I might organize it something like the following:

Code:
set quoteflag = false get the next char from the file While the file is still OK loop if the char is <= 0x20 then if (quoteflag == true) then set quoteflag = false put out the closing quote mark end if else (that is, the char is >= 0x20) if (quoteflag == false) then set quoteflag = true put out the quote mark end if end outer if-else put out the char get the next char from the file end loop

Regards,

Dave
  #5  
Old 19-Sep-2006, 01:54
dadio22 dadio22 is offline
Junior Member
 
Join Date: Sep 2006
Location: Breda, The Netherlands
Posts: 49
dadio22 is on a distinguished road

Re: opening, reading adding writing to a text file


Heey dave,

thnx for the help but now he only puts a quote at the end of the file not the beginning really strange.
my code is something like this now:
CPP / C++ / C Code:
while(!fin.fail() && !fin.eof())
  {
    if (c < 0x20)
	{	
		if (quoteflag = true)
		{
			quoteflag = false;
			cout<< '\'';
	//		fin.get(c);
		}
		cout << c;                 
		fin.get(c);
	}
		
	else	
	{

		if (quoteflag = false)
		{
			quoteflag = true;
			cout<< '\'';
	//		fin.get(c);
		}
			
		cout << c;                 
		fin.get(c);
	}
							
  }

greetz
  #6  
Old 19-Sep-2006, 03:32
dadio22 dadio22 is offline
Junior Member
 
Join Date: Sep 2006
Location: Breda, The Netherlands
Posts: 49
dadio22 is on a distinguished road

Re: opening, reading adding writing to a text file


Here you can see my output.
As you can see he only puts one quote not two and by those lines only at the end not the beginning don't know how to solve this problem "sigh"
Attached Images
File Type: jpg program.JPG (14.0 KB, 11 views)
  #7  
Old 19-Sep-2006, 09:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,303
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: opening, reading adding writing to a text file


Quote:
Originally Posted by dadio22
my code is something like this now:
CPP / C++ / C Code:

	{	
		if (quoteflag = true)

Use the boolean operator '==' for comparison. The single '=' assigns the value true to quoteflag each time (and then executes whatever follows).

Regards,

Dave
  #8  
Old 19-Sep-2006, 09:28
dadio22 dadio22 is offline
Junior Member
 
Join Date: Sep 2006
Location: Breda, The Netherlands
Posts: 49
dadio22 is on a distinguished road

Re: opening, reading adding writing to a text file


Heey dave,

thnx for that pointer can't believe I missed it sjeesh it works now
so now I'll try writing it to another text file.

I'll keep you posted on my progress hopefully this will work better

and thnx again!!!!

greetz Dadio22
  #9  
Old 20-Sep-2006, 03:44
dadio22 dadio22 is offline
Junior Member
 
Join Date: Sep 2006
Location: Breda, The Netherlands
Posts: 49
dadio22 is on a distinguished road

Re: opening, reading adding writing to a text file


Heey,

My program works almost could someone please look at my write method because I think their's a flaw in that because he writes the last character two times really weird. And does anybody know how I can search for a.txt file on the hardrive and then use it to do my program??
thanx in advance

CPP / C++ / C Code:
#include <iostream>

using std::cout;

#include <fstream>

#include<set>
#include <cstdlib> 
using std::ios;
using std::ifstream;
using std::ofstream;

filelengte();
quotes();
toevoegquotes();
opennieuw();

  ifstream fin;  // Initialise filestream object.
  ofstream fout;
  ifstream in;
  char c,d;
  long begin,end;
  int lengte,maxlengte;
  bool quoteflag = false;
  bool quoteflag2 = false;
  int index = 0;
  char q = '\'' ;


int main()
{
  fin.open("D:/file conversion/test.txt", ios::in);    // Open an input filestream.
  fout.open("D:/file conversion/newtest.txt", ios::out);
  // Check if file opened.
  // fin.fail() returns 1 if there is a fail in the filestream.
  if(fin.fail())
  {
    cout << "Error: Unable to open test.c.\n";
    exit(1);
  }
    if(fout.fail())
  {
    cout << "Error: Unable to open newtest.c.\n";
    exit(1);
  }
	filelengte();
}
// fin.get(c);                    // Get first character for kicks.
   
  // While the stream hasn't failed or reached the end of file, read and display.
	int filelengte()
	{
		fin.seekg (0, ios::end);
		lengte = fin.tellg();
		fin.seekg (0, ios::beg);
		cout << "size is: " << lengte << " bytes.\n";
		quotes();
		return 0;
		
	}
int quotes()
{
	while(!fin.fail() && !fin.eof())
	{	
		fin.get(c);
		if (c < 0x20)
		{
			if ( quoteflag == true)
			{
				quoteflag = false;
				index = index+1;
			}
			
		}
		else
		{
			if (quoteflag == false)
			{
				quoteflag = true;
				index = index+1;				
			}			
		}
	}
if (quoteflag == true)
{
index = index+1;
}

cout<< "quotes: " <<index<< '\n';
maxlengte = (lengte + index);
cout<< "lengte nieuwe textfile: "<<maxlengte << '\n';
fin.clear();
fin.seekg (0, ios::beg);
toevoegquotes();
return 0;
}
int toevoegquotes()
{

while(!fin.fail() && !fin.eof())
	{
   		fin.get(c);
		if (c < 0x20) 
		{	
		if ( quoteflag2 == true)
			{
			quoteflag2 = false;
			cout << q ;
			fout.put(q);
			}
			cout<< c;  
			fout.put(c);
		}
	  else 	
	  {
		if (quoteflag2 == false)
		{
			quoteflag2 = true;
			cout<< q;
			fout.put(q);
			}
			cout << c; 
			fout.put(c);
		 }	     
	}


	fin.close();                  // Always close the stream once done.
	fout.close();
	opennieuw();
	return 0;

}
int opennieuw()
{
in.open("D:/file conversion/newtest.txt", ios::in);

if(in.fail())
  {
    cout << "Error: Unable to open newtest.c.\n";
    exit(1);
  }
	while(!in.fail() && !in.eof())
	{
		in.get(d);
		cout<<d;
	}
	in.close();
		return 0;
}
  #10  
Old 20-Sep-2006, 09:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,303
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: opening, reading adding writing to a text file


Quote:
Originally Posted by dadio22
Heey,

My program works almost could someone please look at my write method because I think their's a flaw in that because he writes the last character two times really weird.

In your first post you had this:

CPP / C++ / C Code:
  fin.get(c);                    // Get first character for kicks.
   
  // While the stream hasn't failed or reached the end of file, read and display.
  while(!fin.fail() && !fin.eof())
  {
    cout << c;                 // Display character.
    fin.get(c);                 // Get the next character from the stream.
This is correct. (I don't know why you said that you were getting the first character "for kicks". Doing that way is what makes it work.)

In this one you have the following mistake, repeated in both functions.

CPP / C++ / C Code:
    while (!fin.fail() && !fin.eof()) {
        fin.get(c);
        // stuff that uses the character c
    }

The eof() flag and the fail() flag are set after you try to read past the end of the file.

When you read the last character, the loop uses it, then it does another read. This fails, but you use the character anyhow. (The variable c still has the value from the last valid get().)

You could go back to the original method (cin.get() before entering the loop, or you could do something like the following:

CPP / C++ / C Code:
    while (!in.fail() && !in.eof()) {
        in.get(c);
        if (!in.fail()) {
           // do stuff here that uses the character
        }
    }

If you ever have file input inside a loop, you must test the state of the fstream object after trying to read and before trying to so something with whatever you were reading.

Also, I really don't like the use of global variables in your latest program. It may seem like an expedient thing to do (and it may not be too hard to keep track of them in a small program), but it's generally not considered to be a good idea. (Makes debugging harder, and makes maintenance much more risky if/when the program is ever revised at some later time.)

fstream objects can be passed by reference to the routines that need access. That's the proper way to have different functions that need access to a particular variable.

Regards,

Dave
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Interface for opening and displaying text file ladyscarlet99 MS Visual C++ / MFC Forum 0 29-Sep-2005 03:34
Reading and Writing to a text file raptorhawk C++ Forum 16 14-Apr-2005 14:09
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:30.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.