I am still new with fstream topic and I have some enquiries about using it.

Based on the tutorial shown, I managed to understand a little about it but when I copy the first few lines,

#include <fstream.h>

void main
{
    ofstream file;

    file.open("file.txt");      //open a file

    file<<"Hello file\n"<<75;       //write to it

    file.close();               //close it
}

it show me an error of

error: invalid function declaration 

and also

#warning This file includes at least one deprecated or antiquated header. \
Please consider using one of the 32 headers found in section 17.4.1.2 of the \
C++ standard. Examples include substituting the <X> header for the <X.h> \
header for C++ includes, or <iostream> instead of the deprecated header \
<iostream.h>. To disable this warning use -Wno-deprecated.
#endif

please help me out, experts of C++.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

There's a few issues with your file.

void main

If you're using C++ then main has a return of int not void. Also, you forgot the "()" at the end. So it should be:

int main()

The following compiles and works fine on my system:

#include <fstream>

using namespace std;

int main()
{
    ofstream file;

    file.open("file.txt"); //open a file

    file << "Hello file\n" << 75; //write to it

    file.close(); //close it
    
    return 0;
}

Thank you very much guys.

And also, I have some more questions that I feel that I needed to ask to make the thing simpler.

Some times when I type some of the programs using fstream, it shows error like this:


error: expected ';' before "namespace"

error: expected primary- expression before "namespace"

error: a function-definition is not allowed here before '{' token

Please reply me thanks. I keep getting these errors but I don't know what make them.

here is an example:


#include <iostream>
using namespace std;
#include <fstream>
int main( )
{
int num, total=0;
ifstream myFile("c:\\data.txt");
while (___________)
{
myFile >> num;
total += num;
}
cout << total;
myFile.close( );
return 0;
}
}

There are 2 obvious errors:

a) You have an extra "}" at the bottom that doesn't have an opening brace

b) "using namespace std;" goes after your includes

#include <iostream>
#include <fstream>

using namespace std;

I see. But based o what I have learned about while loop, you are supposed to place some inputs like example:

while(i=0;i<num;i++)

but in this case, I am no supposed to add any new values like i or count. What value should I use?
Am I supposed to use the myFile?<-- (Not quite sure because if I am not wrong, myFile is something like a cin operator)

While loops wait until a condition becomes false.

int a = 0;
while (a < 10)
{
   a++;
}

The main idea of the while loop is to keep on executing while some condition is true. You can use it when you don't know exactly how many times you need to run a loop such as if you ask the user if he/she wants to continue or quit.

A for loop is used to run an exact number of times.

for (int i = 0; i < 10; i++)
{

}

Here it runs so long as i is smaller than 10 and adds 1 for ever loop.

You can always change:

ifstream myFile("c:\\data.txt");
while (___________)
{
myFile >> num;
total += num;
}

to

ifstream myFile("c:\\data.txt");

while (myFile >> num)
{
     total += num;
}

Which would continue while there is data in your file.

you can also use only the

fstream test;
test.open("name of file")
while(!test.eof)//.eof means while is not the end of file.
{
   //condition
}

But after several trying, I am still unclear about the displaying the output on a notepad. I have tried to use the ofstream to output a display into notepad form but these are the results that I usually get:


it showed nothing either on the codeblock window(black window) and also no notepad created and appeared.

it show the output only in the codeblock window(the black window).


It would be appreciated if any of you can help me find the problems and generate a sample running program which can read and write something on the textpad through using the codeblock.

Thank you very much C++ Experts.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream outputFile;

    //Open a file for "Output only"
    //AKA you can only write to this file
    //Create it if it doesn't exist
    outputFile.open("somefile.txt");

    //Write to the file
    outputFile << "Hello World\n";

    //Close the file
    outputFile.close();
    
    return 0;
}

This example only opens a file and writes to it. Reading a file is much the same, except instead of using ofstream you use ifstream.... and the arrows go the other way inputFile >> myVariable;

Now I realize that the program that you're trying to do is more complicated than this, but my simple example gives you a start. If you want more help then show me your source code (well documented preferably so I don't have to guess what you're trying to do).

Also, note that here we're working with files. Therefore, nothing will display to your screen. All the output goes straight to the file.

Aafter I run the file, only the black window appeared, the notepad didn't appeared and btw, very thank you for those who have helped me. But I am afraid may need more help in future to clearly understand these things:)

Aafter I run the file, only the black window appeared, the notepad didn't appeared and btw, very thank you for those who have helped me. But I am afraid may need more help in future to clearly understand these things:)

That's what I said. It's writing to a file not to the screen. So all you'll see is a DOS like screen with no output to it at all. Also, read the comments in the program. They tell you exactly what it's doing. If you need more help then try to make your own program and let us see your code. We can help you more when we see what you are trying to do. Maybe you're just trying to tackle a program that's too difficult for you right now. Remember that programming, like any other skill, is developed over time. Practice some easy programs and work your way up.

Ok. I will try out first, any questions I will post here. Thanks for the help and time:)

This article has been dead for over six months. Start a new discussion instead.