user466534
user466534

Reputation:

why this code writes can't open file

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc,char *argv){
fstream inout("C:\\Users\\7\\Desktop\\test.txt",ios::in | ios::out | ios::binary);
if (!inout){
 cout<<" cannot open input file.\n";
  return 1;

}

long e,i,j;
char c1,c2;
e=5;
 for (i=0,j=e;i<j;i++,j--){


  inout.seekg(i,ios::beg);
  inout.get(c1);
  inout.seekg(j,ios::beg);
  inout.get(c2);
  inout.seekp(i,ios::beg);
  inout.put(c2);
  inout.seekg(j,ios::beg);
  inout.put(c1);
 }


 inout.close();



 return 0;
}

why this code writes can't open file EDIT: i have made corrections but here is one problem in test.txt is written such thing

maiko
miyvarxar
shen
me

so it should write me shen miyvarxar maiko but it does not write anything please help

Upvotes: 0

Views: 2324

Answers (2)

matt_h
matt_h

Reputation: 1339

This seems to work for me:

using namespace std;
int main() 
{ 
  fstream inout("C:\\Users\\turdfurguson\\Turds\testfile.txt", ios::in | ios::out | ios::binary);
  if (inout.good())
    cout << "OK!" << endl;
}

Provided you have a "C:\Users\turdfurgson\Turds\testfile.txt" file that is readable and writable.

Upvotes: 2

Yippie-Ki-Yay
Yippie-Ki-Yay

Reputation: 22854

The code you've provided looks fine.

You may have supplied the wrong path or something like that.

You could also try attempting to open that file in read only mode and see if that is ok:

std::ifstream in("path", std::ios::binary);

if (!in) {
   // fail
}

Upvotes: 0

Related Questions