Forums

Forums (http://www.abandonia.com/vbullet/index.php)
-   Programming (http://www.abandonia.com/vbullet/forumdisplay.php?f=25)
-   -   File I/o Problem In C/c++ (http://www.abandonia.com/vbullet/showthread.php?t=4740)

Kon-Tiki 28-04-2005 05:27 PM

I'm refreshing my C/C++ skills and have encountered something that's stumped me. Dunno why it does this...
I open a .txt file I made with MsDos Editor in my program and use cout to show it. Instead of showing all the text in there, it shows a hex string (0x77fd44). Anybody know why this is?

Kon-Tiki 28-04-2005 05:46 PM

It's starting to come back to me... am fixing it now :bye:

Data 28-04-2005 05:53 PM

you are outputting the fileobject to cout instead of it's contents.

Data 28-04-2005 06:01 PM

try this:

Code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
 *cout<<"Which file do you want to open?" << endl;
 *string name;
 *cin >> name;
 *cout << "Opening "<< name << "..." << endl;
 *ifstream file (name.c_str());
 *cout << file.rdbuf();
 *cin.get();
 *return 0;
}


Kon-Tiki 28-04-2005 06:02 PM

Yup, noticed. I added a do-while loop that takes a line, puts it in a string and couts that, and that till the line's "". Might be buggy when the text looks like this:

Code:

Hello,

My name is Bob.

I like cheese.


Data 28-04-2005 06:07 PM

with file exists check:

Code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
 *cout<<"Which file do you want to open?" << endl;
 *string name;
 *cin >> name;
 *cout << "Opening "<< name << "..." << endl;
 *ifstream file (name.c_str());
 *if(!file) {
 * * cout <<"file: "<< name << " doesn't exist" <<endl;
 * * return 1;
 *}

 *cout << file.rdbuf();
 *cin.get();
 *return 0;
}



The current time is 01:31 AM (GMT)

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.