View Full Version: writing/reading from files

C++ Learning Community > C++ Help > writing/reading from files


Title: writing/reading from files
Description: 0x76fd4ca ?????


ih8censorship - June 24, 2003 09:44 PM (GMT)
what im trying to do is display the contents of an existing file with the addition of a new thing i tryed this and got 0x76fd4ca now the a at the end makes sense because the new edition to the file was "a". the 0x76fd4c part of it was i think the previous part of the file which was (i think) an "h" but in machine code or something.

its like

cout<<file<<charvariable;

anyway i think i got my self in somthing thats a little too advanced for me at this point i think, but it seems so simple.

Shadow of the Moon - June 24, 2003 10:03 PM (GMT)
0x76fd4c is an address in memory, so you are printing a pointer. It's probably because of an array or string.

ih8censorship - June 25, 2003 05:29 PM (GMT)
well heres my code:


CODE

#include <iostream.h>
#include <fstream.h>
ifstream fin;   //what i use for filein, from fstream.h

int main()
{

fin.open("file.txt");
cout<<fin<<"\n";
fin.close();
system("pause");

}


how do i turn the contents of file.txt into a string? im pretty sure thats what the problem is.

dr voodoo - June 25, 2003 06:14 PM (GMT)
this works:
QUOTE
#include <iostream>//I removed the ".h" because that gives me a compiler error
#include <fstream>
#include <string>//So we can use the string var type
using namespace std;//You also forgot this. I don't know what it does but each time I leave it aside I get an error
ifstream fin;

int main()
{
string str;//I use here a string because as far as I know strings aren't limited in size
char file[64];//fin.open needs char and as far as I know can windows path have more than 64 chrarachters
cout << "Enter a text file to open" << endl;
cin >> file;
fin.open(file);
fin >> str;//I dont know if "str<<fin" is the same best check it out
cout << str << endl;
fin.close();
system("pause");
}

Dragon - June 25, 2003 06:32 PM (GMT)
Check out my topic in the C++ Tips forum. It might help.

QUOTE
#include <iostream>//I removed the ".h" because that gives me a compiler error


There is no ".h" on iostream.

dr voodoo - June 25, 2003 06:55 PM (GMT)
QUOTE
CODE
#include <iostream.h>

He had putten one
QUOTE
CODE
char file[64];//fin.open needs char and as far as I know can windows path have more than 64 chrarachters

That should be "have not more".




* Hosted for free by InvisionFree