# C programming question!



## binsky3333 (Feb 16, 2009)

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:


```
#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?


----------



## Disparia (Feb 16, 2009)

```
FILE * fopen(const char * filename, const char * mode);
```

Yer passing 1 too many variables I believe.


----------



## binsky3333 (Feb 16, 2009)

How can i fix this?


----------



## FordGT90Concept (Feb 16, 2009)

Exclude the last argument:

```
fp=fopen("%s", "a+w");
```


----------



## binsky3333 (Feb 17, 2009)

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.


----------



## Disparia (Feb 17, 2009)

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:


```
$fn = 'tehfilename.txt';
$fp = fopen($fn, 'a+t');
$txt = 'this is some text!';
fwrite($fp, $txt);
fclose();
```


----------



## FordGT90Concept (Feb 17, 2009)

Try

```
fp=fopen(open, "a+w");
```


----------



## binsky3333 (Feb 18, 2009)

Thanks that worked!


----------



## binsky3333 (Feb 19, 2009)

Ut oh guys... It looks like i have another problem...

Here is my 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);


----------



## MrSeanKon (Feb 19, 2009)

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


----------

