Input-Output File--Can't create a file...

This is a discussion on Input-Output File--Can't create a file... within the C++ Programming forums, part of the General Programming Boards category; I use following syntax to create a file but file can't be created Code: path1 = "D:\Example\InputFile.txt"; fstream InOutFile(path1,ios::in|ios::out); if ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    52

    Input-Output File--Can't create a file...

    I use following syntax to create a file but file can't be created
    Code:
    path1 = "D:\\Example\InputFile.txt";
    fstream InOutFile(path1,ios::in|ios::out);
    	if (!InOutFile){
    		cerr<<"File can't be created!"<<endl;
    		exit(1);
    	}
    When InOutFile is false??I can't create a file because InOutFile is always false...?Why?Please help me , thanks very much....
    I declared suitable librarys....And compiling doesn't have any errors
    Last edited by zaracattle; 10-17-2006 at 08:59 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> D:\\Example\InputFile.txt
    Should be D:\\Example\\InputFile.txt.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    compiling doesn't have any errors
    Turn up your warning levels. (-W -Wall for GCC.) You should get a message like "unknown escape sequence `\I'", unless \I is a compiler extension and therefore does exist. (Compilers are free to make uppercase escape sequences do what they want.)

    [edit] Instead of using fstream, use ifstream, for input file stream. [/edit]

    [edit=2] CProgramming.com Tutorial: Lesson 10: C++ File I/O [/edit]
    Last edited by dwks; 10-17-2006 at 11:11 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> Instead of using fstream, use ifstream, for input file stream.
    The OP is pretty clearly trying to open the file for both input and output, which requires fstream.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Oops, I missed that. Maybe I should read the tutorial.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Quote Originally Posted by Daved
    >> D:\\Example\InputFile.txt
    Should be D:\\Example\\InputFile.txt.
    I'm using Visual C++ Express Edition..
    I remain have this error...this is my full code:

    "ClassType.h"

    Code:
    #ifndef CLASSTYPE_H
    #define CLASSTYPE_H
    #include "TypeData.h"
    #include <fstream>
    using namespace std;
    
    class Data{
    public:
    	void duongdan1(char * );
    
    	void duongdan2(char * );
    
    	void NhapChoice();
    	void TextFile(fstream&);
    	void UpdateRecord(fstream&);
    	void NewRecord(fstream&);
    	void DeleteRecord(fstream&);
    	void OutputLine(ostream&,const TypeData&);
    	int getAccount(const char*);
    private:
    	int choice;
    	char * path1,*path2;
    };
    
    #endif
    "TypeData.h"

    Code:
    #ifndef TYPEDATA_H
    #define TYPEDATA_H
    struct TypeData{
    	int accountName;
    	char name[100];
    	double balance;
    };
    #endif

    "ClassType.cpp"


    Code:
    #ifndef CLASSTYPE_CPP
    #define CLASSTYPE_CPP
    #include "ClassType.h"
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void Data::duongdan1(char * dd1){
    	path1 = dd1;
    }
    
    
    void Data::duongdan2(char * dd2){
    	path2 = dd2;
    }
    
    void Data::NhapChoice(){
    	fstream XuatNhapFile(path1,ios::in|ios::out);
    	if (!XuatNhapFile){
    		cerr<<"File khong the tao!"<<endl;
    		exit(1);
    	}
    
    	cout<<"\nNhap lua chon cua ban\n"
    		<<"1 - Du Tru 1 dinh dang file text\n"
    		<<"2 - Cap nhat 1 tai khoan moi\n"
    		<<"3 - Them 1 tai khoan moi\n"
    		<<"4 - Xoa 1 tai khoan\n"
    		<<"5 - Ket thuc chuong trinh\n";
    	cin>>choice;
    	while (choice != 5){
    		switch(choice){
    			case 1:
    				TextFile(XuatNhapFile);
    				break;
    			case 2:
    				UpdateRecord(XuatNhapFile);
    				break;
    			case 3:
    				NewRecord(XuatNhapFile);
    				break;
    			case 4:
    				DeleteRecord(XuatNhapFile);
    				break;
    			default:
    				cerr<<"Lua chon sai";
    		}
    		//Reset end of file 
    		XuatNhapFile.clear();
    	}
    }
    
    void Data::TextFile(fstream &DocFile){
    	ofstream XuatFile(path2,ios::out);
    	if (!XuatFile){
    		cerr<<"File khong mo duoc!";
    		exit(1);
    	}
    	XuatFile<<setiosflags(ios::left)<<setw(10)
    		<<"Account" <<setw(22)<< "Name"
    		<<setiosflags(ios::left)
    		<<setw(10)<<"Balance"<<endl;
    	DocFile.seekg(0);
    	TypeData dulieu;
    	DocFile.read(reinterpret_cast<char *>(&dulieu),sizeof(TypeData));
    	while(!DocFile.eof()){
    		if(dulieu.accountName != 0)
    			OutputLine(XuatFile, dulieu);
    		DocFile.read(reinterpret_cast <char *>(&dulieu),sizeof(TypeData));
    	}
    }
    
    void Data::UpdateRecord(fstream &UpdateFile){
    	TypeData dulieu;
    	int account = getAccount("Nhan 1 tai khoan de update!");
    	UpdateFile.seekg((account-1) * sizeof(dulieu));
    	UpdateFile.read(reinterpret_cast <char *>(&dulieu),sizeof(TypeData));
    	if (dulieu.accountName != 0){
    		OutputLine(cout,dulieu);
    		cout<<"\nNhan thay doi tang hoac giam!";
    		float chuyentai;
    		cin>>chuyentai;
    		dulieu.balance += chuyentai;
    		OutputLine(cout,dulieu);
    		UpdateFile.seekp((account-1) * sizeof(dulieu));
    		UpdateFile.write(reinterpret_cast<const char *> (&dulieu),sizeof(TypeData));
    	}
    	else
    		cerr<<"Account #"<<account
    		<<"has no information."<<endl;
    }
    
    void Data::NewRecord(fstream &insertFile){
    	TypeData dulieu;
    	int account = getAccount("Nhan 1 tai khoan moi!");
    	insertFile.seekg((account-1) * sizeof(dulieu));
    	insertFile.read(reinterpret_cast <char *>(&dulieu),sizeof(TypeData));
    	if(dulieu.accountName == 0){
    		cout<<"Nhap name: ";
    		cin>>dulieu.name>>dulieu.balance;
    		dulieu.accountName = account;
    		insertFile.seekp((account-1) * sizeof(dulieu));
    		insertFile.write(reinterpret_cast <const char *>(&dulieu), sizeof(TypeData));
    	}
    	else
    		cerr << "Account #" <<account
    		<<" already contains information."<<endl;
    }
    
    void Data::DeleteRecord(fstream & deleteFile){
    	TypeData dulieu;
    	int account = getAccount("Nhan 1 tai khoan de xoa!");
    	deleteFile.seekg((account-1) * sizeof(dulieu));
    	deleteFile.read(reinterpret_cast <char *>(&dulieu),sizeof(TypeData));
    	if (dulieu.accountName != 0){
    		TypeData dulieu1 = {0,"",0.0};
    		deleteFile.seekp((account-1) * sizeof(dulieu));
    		deleteFile.write(reinterpret_cast <const char *>(&dulieu1), sizeof(TypeData));
    		cout<<"Account #"<<account<<"deleted"<<endl;
    	}
    	else
    		cerr<<"Account #"<<account<<" is empty."<<endl;
    }
    
    void Data::OutputLine(ostream &output, const TypeData &c){
    	output<<setiosflags(ios::left)<<setw(10)
    		<<c.accountName<<setw(22)<<c.name
    		<<setw(11)<<setprecision(2)<<resetiosflags(ios::left)
    		<<setiosflags(ios::fixed|ios::showpoint)
    		<<c.balance<<"\n";
    }
    
    int Data::getAccount(const char *nhac){
    	int account;
    	do{
    		cout<<nhac<<"(1 - 100): ";
    		cin>>account;
    	}while(account<1||account>100);
    	return account;
    }
    
    #endif
    "Use.cpp"
    Code:
    #include "stdafx.h"
    #include "ClassType.h"
    #include "TypeData.h"
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	Data data;
    	char * dd1 = "D:\\Example\\InputFile.txt";
    	char * dd2 = "D:\\thu\\OutputFile.txt";
    
    	data.duongdan1(dd1);
    
    	data.duongdan2(dd2);
    	data.NhapChoice();
    	return 0;
    }
    I want to create a text file,input data into this file,then I use a function to read data from that file and process these datas.... After that I want to output data(result) in another text file...what should I do?...

    Everybody can tell me a sample code that can be used to do this ? thanks very much.....
    Last edited by zaracattle; 10-17-2006 at 05:52 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char * path1,*path2;
    Are you allowed to use the string class?
    Code:
    #include <string>
    .
    .
    	string path1, path2;
    Otherwise you'll have to allocate memory for path1 and path2 (or change them to char arrays) and use strcpy to copy into them.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    sorry,I use declaration:
    fstream XuatNhapFile(path1,ios::in|iosut);
    and path1 must be char *
    and can you type more clearly about using strcpy to copy them?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    I don't think you have to worry about copying them with strcpy since you never change them in the class, although you should make them const char* just to make sure you don't. You can pass around const char* pointers as much as you like as long as you aren't expecting them to change.

    However, using the string class is still a good idea. It works fine with fstream, you just have to use the c_str() function: fstream XuatNhapFile(path1.c_str(),ios::in|ios:out);

    >> I remain have this error...
    Does that mean that the file still doesn't open for you? What if you do try it with just an ifstream without the ios:: flags since it looks like you won't be using that file for output after all?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Quote Originally Posted by Daved
    Does that mean that the file still doesn't open for you? What if you do try it with just an ifstream without the ios:: flags since it looks like you won't be using that file for output after all?
    Yes.....It still doesn't create a file....I don't know why....

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Does the file exist at that place on your hard drive? Does the following program display success or failure?
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
      ifstream inf("D:\\Example\\InputFile.txt");
      if (!inf)
        cout << "Failure!\n";
      else
        cout << "Success!\n";
    }
    Since you are using an absolute path, it should work if the file is there on your hard drive. If that isn't the exact string you are using, then maybe you should show us the exact path you are using.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Outputting to a File Modified Text
    By alpha in forum C++ Programming
    Replies: 8
    Last Post: 11-24-2003, 07:39 PM
  4. file input and output
    By isaac in forum C Programming
    Replies: 3
    Last Post: 06-04-2002, 04:41 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21