readBytes function

I cant understand why this wont work. I'm thinking it cant find the file"text.txt". I keep getting the error message.








#include <iostream>
#include <fstream>

using namespace std;

//function prototype
void readBytes(ifstream&, long, int);

int main()
{
//declare constants and variables
const int MAXLENGTH = 35;
char filename[MAXLENGTH] = "test.txt";
long offset;
int num_chars;
ifstream inFile;

//open file and test for success.
inFile.open(filename);
if (inFile.fail())
{
cout << "\nERROR Opening: " << filename << endl;
system("pause");
exit(1); //EXITS PROGRAM
}

//get offset and number of characters
cout << "Please choose the offset: ";
cin >> offset;
cout << "Please enter how many characters to read: ";
cin >> num_chars;

//function call
cout << "You have selected:\n";
readBytes(inFile, offset, num_chars);
cout << endl;

system("pause");
return 0;
}

void readBytes(ifstream& fileIn, long start, int length)
{
char ch;

fileIn.seekg(start, ios::beg);

for (int i=0; i< length; i++)
{
ch = fileIn.get();
cout << ch;
}
return;
}
First of all, your program has "test.txt" not "text.txt"

In case that was just a typo on your part, have you confirmed that the file is indeed in the working path of the program? Perhaps try using an absolute path name.

Check the file permissions - can you open it using a text editor?

Finally, please format your code when posting, it does help.
Topic archived. No new replies allowed.