| View previous topic :: View next topic |
| Author |
Message |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
Posted: Thu Jun 05, 2008 5:09 am Post subject: Need help on reading from files |
|
|
So far i got this code:
| Code: | #include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdlib>
int main()
{
ifstream indata;
int num;
indata.open("C:\\Documents and Settings\\KPO\\Dokumenter\\Visual Studio 2008\\Projects\\readfromfile\\Debug\\example.evt"); // opens the file
if(!indata) {
cerr << "Error: file could not be opened" << endl;
std::cin >> num;
exit(1);
}
char word[99];
indata.getline(word, 10, '\n');
std::cout << word << std::endl;
indata.close();
std::cin >> num;
return 0;
} |
and what i wish to do, is to repeat this part:
| Code: | indata.getline(word, 10, '\n');
std::cout << word << std::endl;
|
Untill there is no more lines in the file, how would i do this? |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Thu Jun 05, 2008 5:18 am Post subject: |
|
|
| Code: | #include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdlib>
int main()
{
ifstream indata;
int num;
indata.open("C:\\Documents and Settings\\KPO\\Dokumenter\\Visual Studio 2008\\Projects\\readfromfile\\Debug\\example.evt"); // opens the file
if(!indata) {
cerr << "Error: file could not be opened" << endl;
std::cin >> num;
exit(1);
}
char word[99];
while(!indata::eof()){
indata.getline(word, 10, '\n');
std::cout << word << std::endl;
}
indata.close();
std::cin >> num;
return 0;
} |
_________________
|
|
| Back to top |
|
 |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
Posted: Thu Jun 05, 2008 5:58 am Post subject: |
|
|
Wow, ty so much!
but you just made one error... its indata.eof and not indata::eof
edit: Now it doesent write anything at all O.o, actually it just freezes O.o... |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Thu Jun 05, 2008 10:59 am Post subject: |
|
|
http://www.cplusplus.com/doc/tutorial/files.html
| Code: | // reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
} |
So check to see if myfile is open and if it is and it isn't the end of the file get a line. _________________
|
|
| Back to top |
|
 |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
Posted: Thu Jun 05, 2008 4:01 pm Post subject: |
|
|
Thanks, the link you gave me was good , ill try it out tomorrow!  |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Thu Jun 05, 2008 5:41 pm Post subject: |
|
|
Chopped up version using TinyXML's method with fopen:
| Code: | #include <windows.h>
#include <stdio.h>
#include <tchar.h>
int main( int argc, TCHAR* argcv[] )
{
// Open File For Reading
FILE* fFile = 0;
errno_t errNum = fopen_s( &fFile, _T("test.txt"), _T("rb") );
if( (errNum != 0) || (fFile == 0) )
return 0;
// Get File Size
long lLength = 0;
fseek( fFile, 0, SEEK_END );
lLength = ftell( fFile );
fseek( fFile, 0, SEEK_SET );
// Allocate Buffer If File Is Not Empty
if( lLength <= 0 )
{
// Empty
fclose( fFile );
return 0;
}
char* buf = new char[ lLength + 1 ];
// Read File Contents
if( fread( buf, lLength, 1, fFile ) != 1 )
{
// Failed..
delete[] buf;
return 0;
}
// Set Null Char To Terminate String
buf[ lLength ] = '\0';
// Output Buffer
printf( buf );
// Delete Buffer And Cleanup
delete[] buf;
fclose( fFile );
getchar(); // Pause Without iostream
return 0;
} |
_________________
- Retired. |
|
| Back to top |
|
 |
|