Thread: Parameters

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    184

    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
    Last edited by gvector1; 04-04-2003 at 12:47 PM.

  2. #2
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    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???
    guns dont kill people, abortion clinics kill people.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parameters quick Question
    By lifeis2evil in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2007, 11:12 PM
  2. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  3. Additional parameters for operator delete
    By darksaidin in forum C++ Programming
    Replies: 0
    Last Post: 09-21-2003, 11:46 AM
  4. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  5. command-line parameters.
    By Tombear in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 08:40 AM