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 01-Mar-2004, 11:14
mgp6q mgp6q is offline
New Member
 
Join Date: Mar 2004
Posts: 6
mgp6q is on a distinguished road
Angry

reading and writing to a file in C++


I need to write a small program that reads a file and adds somethings to it. The file contains file paths. This is what it needs to look like

1
i:\home8\test_maintenance.drw\02_test.fld\02PC342M .dwg
i:\home8\test_maintenance.drw\02_test.fld\02PC342M
1

right now i just have
i:\home8\test_maintenance.drw\02_test.fld\02PC342M .dwg
in a file

The code below is what i have so far. Any help?? thanks

CPP / C++ / C Code:
//getline(array,array_size,delim);

#include <iostream>
#include <fstream>

int main()
{

	std::fstream iofile("input2.txt", 
			std::ios::in | std::ios::out);
	if (!iofile.fail()) {
		while (!iofile.eof()) {
			
			static char path[1000];	   	   
			iofile.getline(path);
			
			if (!iofile.eof())
				
				iofile << path;
			

		}
	}
	return 0;
}
Last edited by dsmith : 01-Mar-2004 at 12:59. Reason: Added C syntax highlighting
  #2  
Old 01-Mar-2004, 12:03
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,432
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Do you care how many lines are already in the file before writing a new line? If not, look into the append instead of /in addition to in and out for fstream.
  #3  
Old 01-Mar-2004, 12:25
mgp6q mgp6q is offline
New Member
 
Join Date: Mar 2004
Posts: 6
mgp6q is on a distinguished road
No it doesn't matter how many lines


Quote:
Originally Posted by WaltP
Do you care how many lines are already in the file before writing a new line? If not, look into the append instead of /in addition to in and out for fstream.
  #4  
Old 01-Mar-2004, 13:17
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
I think it may be easier to open a seperate file for reading and a seperate file for writing and then copy the output file to the input file.

Then you should be able to do something like this:

CPP / C++ / C Code:
outfile << "1\n";
outfile << path;
*(path + (strlen(path) - 4) ) = 0 //Take out .dwg text
outfile << path;
outfile << "1\n";

Please note that I haven't tested that. I would make sure that your output file is correct before putting in the code to copy it over your input file.
  #5  
Old 01-Mar-2004, 14:35
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
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 beholddavekw7x is a splendid one to behold
If you want to append lines to a file, you can open it for reading
and writing, then write to the end of the file (no need to read
the lines that already in the file).

You could use iofile.seekg() to move around in the file, but that's
not necessary if you open the file with the pointer already at the
end.

Maybe this will help (note that you can eliminate having to write
"std::" everywhere by putting the statement "using namespace std;"
just before your main() statement).

Also, it's always a good idea to see whether the file was opened
properly.

The following compiles and executes OK on my Windows machine with
Borland Version 5.5 and g++ version 3.3.1


CPP / C++ / C Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{

  fstream iofile("input2.txt", ios::in | ios::out |ios::ate);
  if (!iofile.fail()) { // Put whatever you need to generate new lines here

    for (int i = 1; i <= 3; i++ ) {
      iofile << "Put new line #" << i << " at end" << endl;
    }
  }
  else {
    cout << "Failed to open file <input2.txt> " << endl;
  }
  return 0;
}



Dave
  #6  
Old 01-Mar-2004, 14:59
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 570
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Here's a tip to help you get rid of all that std wrapping.

For stream I/O, you can simply test for true or false to see if an error occured while opening the file, or if a read was successful. Here's an example of that:
CPP / C++ / C Code:
if (!iofile) // this test returns false if anything went wrong
  {
    cout << "Woops! I couldn't open the I/O file. Did you type the path correctly?\n";
    return 0;
  }
else
  . . .

iofile >> myVar;
while (iofile) // true if the read was successful, false if otherwise or end-of-file
  {
     cout << "Here's what I found in the file:" << myVar << endl;
     ioFile >> myVar;
  }
So I really see no point in wrapping your Stream I/O in a Standard I/O class. Stream I/O has plenty of native functions that work perfectly well. Here's how to declare various types of streams using Stream I/O.
CPP / C++ / C Code:
ifstream inFile("C:\\myfiles\\sometext.txt"); // opens sometext.txt for reading
ofstream outFile("C:\\myfiles\\sometext.txt",ios::app); // opens sometext.txt for writing and appending.

//You can also declare your streams independently of their paths as shown below.

ifstream inFile;
ofstream outFile;

inFile.open(filename);
outFile.open(filename,ios::app);

//Don't forget to close them!
inFile.close();
outFile.close();
  #7  
Old 01-Mar-2004, 15:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
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 beholddavekw7x is a splendid one to behold
So, I tried the following:

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

//using namespace std;

int main()
{
  ofstream outFile("sometext.txt",ios::app); // opens for appending

  outFile << "New line\n";

  outFile.close();

  return 0;
}

Neither Borland Version 5.5 or g++ version 3.3.1 compiled.
Error messages indicated that ofstream was undefined (undeclared).


When I un-commented the "using namespace std" statement, it
compiled and executed properly.

I have a lot less experience with C++ than with C, but so far, I have
treated "using namespace std;" as an idiom: just do it.

Is there any reason not to "just do it"?


Dave
  #8  
Old 01-Mar-2004, 17:24
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 570
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
You need to use the .h when including fstream.h, not just <fstream>, otherwise it won't compile. I don't see any problem with using namespace std, unless you don't need it. You only need it if you're going to use std functions on objects outside of the std class. If you write a program that doesn't use any std functions, then there's no need for the namespace statement.
  #9  
Old 01-Mar-2004, 18:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
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 beholddavekw7x is a splendid one to behold
Gnu c (g++ version 3.3.1 will not compile, with or without
the .h.

Here are the g++ errors that prevent the compile with fstream.h
and with the "using namespace std" commented out:

Quote:
yy.cpp: In function `int main()':
yy.cpp:7: error: `ios' undeclared (first use this function)
yy.cpp:7: error: (Each undeclared identifier is reported only once for each
function it appears in.)
yy.cpp:7: error: syntax error before `::' token
yy.cpp:9: error: invalid operands of types `std::ofstream ()(...)' and `const
char[10]' to binary `operator<<'
yy.cpp:11: error: request for member `close' in `outFile(...)', which is of
non-aggregate type `std::ofstream ()(...)'


Dave
  #10  
Old 01-Mar-2004, 18:40
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 570
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Why is it complaining about std:: if you don't have any in there? There must be something wrong. I've never tried G++. The exact code herein compiles just fine with no warnings under Borland 6 and VC 6.
CPP / C++ / C Code:
#include <fstream.h>

int main()
{
  ofstream outFile("sometext.txt",ios::app); // opens for appending

  outFile << "New line\n";

  outFile.close();

  return 0;
}
 
 

Recent GIDBlogGID Spam Detector 1.1.0 by gidnetwork

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
Linux Kernel Upgrade Mini Howto dsmith Computer Software Forum - Linux 4 31-Mar-2011 13:45
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 21:07

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

All times are GMT -6. The time now is 23:49.


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