-
Parameters
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
-
if your program is supposed to accept the names of two files to open, where does it get them from? does main have to accept the names or can it be another function you write? when i compiled and ran it crashed, but i think it's because it's trying to open the files that are being passed to it and simply not knowing or not finding where they are???
-
I have been running the program from a command line.
I compile using .NET and go to a command prompt in the debug folder for the program. I then call it like this:
"File Write" readfile writefile
This has been working with me.
Thanks for the reply,
Kendal