473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

file operations in Dev C++

The fstream class is not creating a file when file.open(<file name>) is used. Why??
Oct 11 '07 #1
6 13629
Meetee
931 Expert Mod 512MB
The fstream class is not creating a file when file.open(<file name>) is used. Why??
Can you post whatever you have done so far? It is not the case normally. So by seeing your code we can analyze the problem.
Hope you have done something like this.
Expand|Select|Wrap|Line Numbers
  1.  #include<fstream.h>
  2.  fstream file_op("c:\\test_file.txt",ios::in);
  3.  
Regards
Oct 11 '07 #2
This is the code.


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string a("harsha");
string b("Ratnesh");
string c;
string d;
int x=1000,y;
fstream file;


file.open("TRIAL",ios::in|ios::out);
file.write((char *)&a,sizeof(a));
file.write((char *)&x,sizeof(x));
file.write((char *)&b,sizeof(b));



file.read((char *)&c,sizeof(b));
file.read((char *)&y,sizeof(y));
file.read((char *)&d,sizeof(c));
cout<<c<<endl;
cout<<y<<endl;
cout<<d<<endl;

file.close();

system("pause");
}

Now when I check my working directory, there is no file by name "TRIAL".
The program runs without errors and the output is


<empty line>
0
<empty line>

But if execute the same program with ofstream and ifstream class there is no problem. The code is

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string a("harsha");
string b("Ratnesh");
string c;
string d;
int x=1000,y;
ofstream ofile;


ofile.open("TRIAL");
ofile.write((char *)&a,sizeof(a));
ofile.write((char *)&x,sizeof(x));
ofile.write((char *)&b,sizeof(b));

ofile.close();

ifstream ifile;
ifile.open("TRIAL");
ifile.read((char *)&c,sizeof(b));
ifile.read((char *)&y,sizeof(y));
ifile.read((char *)&d,sizeof(c));
cout<<c<<endl;
cout<<y<<endl;
cout<<d<<endl;

ifile.close();

system("pause");
}


The file TRIAL gets created.
The output is

harsha
1000
Ratnesh

which is correct.
But why is the fstream class not creating the file?

Thank you for the response.
Oct 11 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
But why is the fstream class not creating the file?
Your first example was:
file.open("TRIAL",ios::in|ios::out);
Because you said input or output, the file was not created due to the fact that input files are not created. If they were, they would be output files.
Oct 11 '07 #4
Hi,

But the book says it is mandatory to specify mode with fstream class i.e. when we use open, we should use some ios:: mode.
Moreover the mode has an 'or' operator. The very idea of using fstream is that a file can be opened both for reading and writing at the same time.
Anyway, I'll try without specifying any mode first and then specifying only ios::out mode.

Regards
Oct 12 '07 #5
Hi,
The code


fstream file;
file.open("TRIAL",ios::out);
--
--
--
file.close();
file.open("TRIAL",ios::in);
--
--
--
file.close;

This seems to work fine. But why can't I write into and read from a file at the same time with

file.open("TRIAL",ios::in|ios::out);???


Anyway, thank you for your suggestion.
Oct 12 '07 #6
Hi,

By the way, the program of read/write simultaneously i.e.

file.open("TRIAL", ios::in | ios::out);

works fine in Turbo C++ compiler.

So the problem is with the Bloodshed Dev C++ compiler.

Can somebody tell them to correct it ??

Thanks a lot. I have found my solution. You can close this thread...

Regards.
Oct 12 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
5
by: Shu-Hsien Sheu | last post by:
Hi, Does the seek method would close the file object after using a for loop? My program looks like this: f = open('somefile', 'r') for lines in f: some operations f.seek(0)
3
by: Peter | last post by:
Hello, Two newbie questions: 1) I have a javascript file with a function in it. From this function I want to access a variable in another javascript file -which is not inside a function. I...
3
by: Scott Brady Drummonds | last post by:
Hello, all, My most recent assignment has me working on a medium- to large-sized Windows-based C++ software project. My background is entirely on UNIX systems, where it appears that most of my...
2
by: Eat_My_Shortz | last post by:
I'm trying to interoperate between file IO operations, on an open file, between unmanaged C++ code and Managed C++, using the .NET framework. Basically, at present, I have a C-style FILE* object,...
11
by: Jeevan | last post by:
Hi, I have some data which I am getting from a socket. I am currently storing the data in an array (so that future reading of the data will be fast as it will be in RAM instead of hard disk)....
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
9
by: Julien Biezemans | last post by:
Hi! Here is the problem: I'd like to restrict local filesystem stream operations to one directory just like a root jail. fopen('/file.bin') would actually open /some/path/file.bin. One goal...
5
by: MC | last post by:
I've been using the ASP.NET ad rotator for some time and have been asked to track the hits and click thrus, The solution that seemed obvious to me was to extend the existing control to record the...
0
by: tabassumpatil | last post by:
Please send the c code for: 1.STACK OPERATIONS : Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2. 2.QUEUE OPERATIONS : Write a program to implement...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.