Hello everyone...
Got a problem with a program that accepts parameters.
The program accepts two filename parameters. One for reading and one for writing. It does not matter what order I try to open them in......the program will open any file for read but it will not open a file for write. I have rearranged the file opening order but with no luck. Now if I try to open the file intended for writing as a read file, it opens. Why won't it open a file for writing??????Any suggestions????? Here is the code>>>
Code:
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
FILE *stream, *stream2;
using namespace std;
void main(int argc, char *argv[])
{
//string file1, file2;
//cout<<"Enter filename to read from\t";
//getline(cin,file1);
//cout<< file1;
//cout<<"Enter filename to write to\t";
//getline(cin,file2);
//cout<<file2;
/* Open for read (will fail if file "data" does not exist) */
if( (stream = fopen( argv[2], "a+" )) == NULL )
printf( "The file %s was not opened\n", argv[2] );
else
printf( "The file %s was opened\n", argv[2] );
/* Open for write */
if( (stream2 = fopen( argv[1], "r" )) == NULL )
printf( "The file %s was not opened\n", argv[1] );
else
printf( "The file %s was opened\n", argv[1] );
cout << "Enter text to write to file...\t";
string text;
getline(cin,text);
fprintf(stream2,text.c_str());
}
When I use the code commented out...where the program asks for the filenames, it opens a file for read and a file for write just fine. It just messes up when I pass the filenames as parameters.
Thanks,
Kendal