-1

I wrote this code to input a number from a user and output it to a file .But its is not working ,after running the code the output.txt file is still empty. Please tell me where I have done wrong . I assure that I have created the output.txt file before running the program so the file pointer will not be NULL.

#include <stdio.h>
#include <stdlib.h>
int main (void)
{
FILE *ptr;ptr=fopen("output.txt","rw");
if(ptr==NULL){printf("Error in oppening file aborting .......");exit(0);}
char ch[100];
scanf("%s",ch);
fprintf(ptr,"%s",ch);
fclose(ptr);
return 0;
}
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Tatan
  • 1
  • 2
  • 3
    http://www.cplusplus.com/reference/cstdio/fopen/ : use "r+", "w" or "a", there is no "rw" spec here – Bulat Jun 30 '18 at 11:08
  • 2
    Hint: All C standard library functions have [standardized errors](https://en.wikipedia.org/wiki/Errno.h) which you should check if you ever encounter a problem. – tadman Jun 30 '18 at 19:38
  • works for me, vote to close – Stargateur Jun 30 '18 at 19:45
  • 1
    `{printf("Error in oppening file aborting .......");exit(0);}` has many mistakes. It prints to the wrong stream, it does not provide enough information, and it lies to the OS in claiming that the program succeeded. Change it to `{perror("output.txt"); exit(EXIT_FAILURE);}` – William Pursell Jun 30 '18 at 19:54
  • You need to check `fprintf` and `fclose` for errors, not just `fopen`. – melpomene Jun 30 '18 at 20:08
  • Use `%99s` in your scanf to avoid a buffer overflow ... or use `%ms` and change the argument to to allocate as much as needed – o11c Jun 30 '18 at 20:42
  • @Stargateur report bug to the developers of library. oh, well, fopen can support unportable specs too, at least we have some here in MSVC – Bulat Jul 01 '18 at 01:00
  • @Bulat Oh indeed, look like my OS is very permissive on this one. I always through you could combine these flags. – Stargateur Jul 01 '18 at 07:46

1 Answers1

2

From fopen documentation, the supported access modes are:

"r" read: Open file for input operations. The file must exist.

"w" write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.

"a" append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek, fsetpos, rewind) are ignored. The file is created if it does not exist. "r+" read/update: Open a file for update (both for input and output). The file must exist.

"w+" write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.

"a+" append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseek, fsetpos, rewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.

In your code you use "rw" which is invalid and that's the reason your program doesn't work.

Change "rw" to "w" and your program will work. Note that you don't need to create output.txt, fopen will create it for you if your current user has write privileges in program's directory.

leopal
  • 4,711
  • 1
  • 25
  • 35