Computer problems,Computer help
*AX SOFT>>>Programming & Design

Can you have two or more files open for writing when doing C++ programming?


I'm using this syntax to open the file for writing:
pointertoopenfile = fopen("file.txt","w");

To restate my question, can I open two or more files like this to write to in C++? I would like to open up two or three files at the beginning of my program, be able to write to them throughout the program, and then close them all when I'm done.

It seems like it only works to have 1 file open for writing at a time. My other option is just to open and close each file as I want to write to it and append to previous written text with:

pointertoopenfile = fopen("file.txt","a");

Absolutely yes!
Full Code(How I do it):
#include <fstream.h>
int main ()
{
fstream file1;
fstream file2;
file1.open("file1.txt",ios::out);
file2.open("file2.txt",ios::out);
file1<<"Hi!";
file2<<"Bye!";
file1.close;
file2.close;
return 1;
}

As long as you use a different variable for each file (pointertoopenfile1, pointertoopenfile2, pointertoopenfile3) you won't have any problem.

But, depending on the file type (text, data), you may be able to open the file for append and just add to it. So you can open the file, append, close the file. That way, if the program crashes, or it has to abort, everything it wrote so far is saved.

I have a program that opens and writes to 4 files:

f1 = fopen(File1, 鈥渨鈥?;
f2 = fopen(File2, 鈥渨鈥?;
f3 = fopen(File3, 鈥渨鈥?;
f4 = fopen(File4, 鈥渨鈥?;

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • If you had to sum up the difference between actionscript 2 and 3?

    ...

  • How do you upload a Website made in Microsoft Word onto the internet?

    It's done by FTP (File Transfer Protocol) Just like browsing your windows files. But, you cannot "code" a website in Microsoft Word as the Word will format the text and that can ...

  • How can I add a hyperlink to my craigslist image?

    Craigslist will accept well formed (X)HTML in the description area of your advert. Make sure that you are using the absolute path to the image on the server. Don't forget to include the alt...

  • Utorrent application downloaded. How to use?

    I they are rar files, Right click on the first one and pick 'Extract Here'. That will extract them for you. If they are not rar files just let us know what type they are and we can help y...

  • How do i create next a previous buttons which will update the content withing my browser without going...?

    Look into using iFrames or AJAX. With iFrames you could change the web page that it's displaying without the rest of the page changing.. With AJAX you could re-write the .innerHTML prope...

  • Is web design part of the Web Technology career?

    Yes, but keep in mind that there are a lot of elements of Graphic Artistry involved in web "design". Nowadays most prefessional-level websites are developed by a team. One person or gro...

  • Where I can find a software to allow visitors to upload video on my site and visitors to voting for the videos

    There is many clones of the popular YouTube page. Well, they "clone" it so it looks like YouTube. But ehm, take your time to look at these: ...

  • Is it easier and faster to do animation in Adobe Flash or 3d software like Truespace and maya?

    Much Much easier and faster in Adobe Flash CS3. Speaking from experience here, 3d software like maya and 3dstudiomax etc take an incredible amount of work to do even simple animations. While they l...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster