Hi,

I am currently running on windows and have and issue when i try to read/write files,the problem being that it just doesn't happen.
I know the code is correct because when I use a linux os and other windoze machines everything is ok..it just seems to be my computer.

Im just wondering if anyone has ever come across this issue,or know of anyway to fix it.

Thanks for any help.

I am currently running on windows and have and issue when i try to read/write files,the problem being that it just doesn't happen.

That doesn't give us very much to work with; it would help if you could clarify what "just doesn't happen" means. Is it truly not happening, as in the code appears to work but no files show up, or is it something else? Please post some details about exactly what is happening, including a short example of the code that's failing to work correctly.

Is it in the same directory? Show some code?

What i meant by nothing happens is that no file is ever created,and if i create the file in the same directory nothing is written to it.

I know my code works as when i switch os file handling is not a problem

here is a test code that doesnt work

// A simple program to practice writing and reading from files.

#include <iostream>
#include <fstream>

using namespace std;

void main() {
    
    int age = 30;
    
    cout << "Welcome to the reading and writing to files program.." << endl;
    
    // Declare and open a filestream.
    fstream file ("myFile.dat", fstream::out | fstream::binary | fstream::app);
    
    // write my age to the file
    file.write((char*)&age,sizeof(age));
    
    // flush the buffer
    file.flush();
    
    // Close the file
    file.close();
    
}

bump

Well, I'm using Windows and it gives me an error when I try to compile your program. Hence, no file is created because the program is never run.

main MUST return an int in C++.

As soon as you change void to int and run the program you will notice that it does in fact create a file. If it matters I compiled it under both VC++ and GCC and both compilers created a file under Windows.

This line:

file.write((char*)&age,sizeof(age));

produces 4 blank spaces.

Add this to the bottom of that program to verify that it worked (works ok for me on Windows 7 using VC++ 2008 Express compiler)

ifstream in("myFile.dat", ios::binary);
    int k = 0;
    if( in.is_open() )
    {
        in.read((char *)&k, sizeof(int));
        cout << "k = " << k << '\n';
    }

I know that my main returning an int was not the problem,i've changed to to confirm this aswell.

Also its a binary file,so it's not supposed to read the number characters to the file.

Have you tried using ios:: flags on the file open instead of the fstream:: flags?
Perhaps try:

fstream file ("myFile.dat", ios::out | ios::binary | ios::app);

Does that make any difference??
Cheers for now,
Jas.

...nevermind...

Ok the issues is resolved thanks for all the help.

What turned out to be the problem was that i was not compiling and building my program in the IDE.I was always under the impression that to output to a file all i needed to do was compile and run the .exe

Lesson learned :D

The damed program works fine as it is, its not necessary to change the data type of age, nor would it even be desireable. You're trying to make a binary file act like a text file -- it doesn't. You can not look at a binary file with a text program such as Notepad or Wordpad.

Ok the issues is resolved thanks for all the help.

What turned out to be the problem was that i was not compiling and building my program in the IDE.I was always under the impression that to output to a file all i needed to do was compile and run the .exe

Lesson learned :D

It is NOT necessary to compile a program using an IDE. You can use command-line compiles with or without a makefile if you want to. What compiler was you using and how was you compiling the program (post code to compile).

I was using Dev BloodShed c++,i was compiling by using the exectute tab on the toolbar..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.