1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

C programming question!

Discussion in 'Programming & Webmastering' started by binsky3333, Feb 16, 2009.

  1. binsky3333

    binsky3333 New Member

    Joined:
    Jul 5, 2007
    Messages:
    643 (0.25/day)
    Thanks Received:
    21
    Ok hi everyone, i am currently making a very simple C word processor where you can type in the file name of a text file you want to open, then add text to, though i am having some problems with opening a file. Here is what i have so far:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        char write[256];
        char open[128];
        printf("Please enter the name of the file you want to open.(Does not need to exsist)\n");
        fgets ( open, 128, stdin );
        FILE *fp;
        fp=fopen("%s", "a+w", open);
        printf("Please enter the text you would like to write to the file\n");
        fgets ( write, 256, stdin ); 
        fprintf(fp, "%s\n", write);
        printf("Complete\n");
        getchar();
        
    }
    
    The problem i am having is with the fopen("%s", "a+w", open). The program will not compile because of this... I get... 178 C:\Dev-Cpp\include\stdio.h too many arguments to function `FILE* fopen(const char*, const char*)' . Basically the %s is a reading variable input. Someone types in the filename, then it is inserted into the position of %s so the user can open the file... Why wont this work what am i doing wrong?
    Last edited: Feb 16, 2009
  2. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,410 (1.32/day)
    Thanks Received:
    636
    Location:
    Geneva, FL, USA
    Code:
    FILE * fopen(const char * filename, const char * mode);
    Yer passing 1 too many variables I believe.
  3. binsky3333

    binsky3333 New Member

    Joined:
    Jul 5, 2007
    Messages:
    643 (0.25/day)
    Thanks Received:
    21
    How can i fix this?
  4. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,453 (6.27/day)
    Thanks Received:
    3,419
    Location:
    IA, USA
    Exclude the last argument:
    Code:
    fp=fopen("%s", "a+w");
    Crunching for Team TPU
  5. binsky3333

    binsky3333 New Member

    Joined:
    Jul 5, 2007
    Messages:
    643 (0.25/day)
    Thanks Received:
    21
    Yes... i kno that, but then the user cannot enter the file they would like to open/create. The name is the file comes up as %s, not as what they entered.
    Last edited: Feb 17, 2009
  6. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,410 (1.32/day)
    Thanks Received:
    636
    Location:
    Geneva, FL, USA
    Aren't you already setting it to the variable open? You're essentially trying to call fopen(filename, mode, filename);

    At least that's what I believe is happening. In php, I would use fopen like:

    PHP:
    $fn 'tehfilename.txt';
    $fp fopen($fn'a+t');
    $txt 'this is some text!';
    fwrite($fp$txt);
    fclose();
  7. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,453 (6.27/day)
    Thanks Received:
    3,419
    Location:
    IA, USA
    Try
    Code:
    fp=fopen(open, "a+w");
    Crunching for Team TPU
  8. binsky3333

    binsky3333 New Member

    Joined:
    Jul 5, 2007
    Messages:
    643 (0.25/day)
    Thanks Received:
    21
    Thanks that worked!
  9. binsky3333

    binsky3333 New Member

    Joined:
    Jul 5, 2007
    Messages:
    643 (0.25/day)
    Thanks Received:
    21
    Ut oh guys... It looks like i have another problem...

    Here is my code:
    Code:
    int viewer()
    {
       
      FILE *file;
      char c;
      char fileparam[80];
      char write[256];
      printf("Type the filename (this will create a new file or edit an existing one) = ");
      scanf("%s", fileparam);
      file = fopen(fileparam, "a+w");
      if(file==NULL) {
        printf("Error: can't open file.\n");
        /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
        return 1;
      }
      else {
        printf("File opened successfully\nContents:\n\n");
        
        while(1) {     /* keep looping... */
          c = fgetc(file);
          if(c!=EOF) {
            printf("%c", c);  
            /* print the file one character at a time */
          }
          else {
            break;     /* ...break when EOF is reached */
          }
        }
        printf("\n\n");
    
      getchar();
      printf("Please enter the text you would like to add to the file:");
      fgets( write, 256, stdin );
      fprintf(file, "%s", write);
      printf("successful!\n");
      system("pause");
      apps();
        
    }
    }
    
    
    
    Now the problem i am having is with the apps(); . Whenever i have this in the code, the text that is supposed to be written to the file is not... But whenever i take out apps(); the text is written to the file, but then the program stops because its the end of it... the apps(); is redirecting it to another function in another header file. How can i fix this problem?

    EDIT: Nevermind i fixed the problem i just had to add fclose(file);
    Last edited: Feb 19, 2009
  10. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    Although I am not a professional programmer (I just start programming for making some cardgames + OcBible) always when you open a file for reading/writing you must close it otherwise crashes or abnornal "behavior" of your programs will occur.
    As you said you found your bug :)

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page