Its Zaid
Its Zaid

Reputation: 13

Not creating txt file in c drive by file operations in C language

I am trying to create a simple text file using TurboC++ DOS emulator. But, it's not creating the file. I am referring to an online video where the same file is getting created. What can be the issue?

Below is the code:

#include<stdio.h>
#include<stdlib.h>

int main()
{
  int num;
  FILE *fptr;

  fptr = fopen("c:\\program.txt","w");

  if(fptr == NULL)
  {
    printf("error");
    exit(1);
  }
  printf("enter num = ");
  scanf("%d", &num);

  fprintf(fptr,"%d",num);
  fclose(fptr);

  return 0;

}

Upvotes: 1

Views: 1064

Answers (3)

Kitswas
Kitswas

Reputation: 1197

Although, this answer may no longer be required, I am providing it here for future reference of those who might need it.

I had the same problem some time ago.

It turns out the DOS emulator emulates the PC storage for programs.
The folder C:\TurboC++\Disk is treated as C:\ by TurboC++, and it is the root folder for whatever TurboC++ needs to refer to.

TurboC++ Root

So, your file program.txt, should have the path [Your Installation Path]:\TurboC++\Disk when seen from outside TurboC++.

Upvotes: 1

Its Zaid
Its Zaid

Reputation: 13

I switched DOS emulator to code blocks.

Upvotes: 0

Brecht Sanders
Brecht Sanders

Reputation: 7305

Go to the prompt in the DOS emulator and do something like DIR > C:\program.txt, check if you get an error and if not then check with DIR C:\ if that file is actually created.

If that doesn't work it's not a problem with your C application, but with the possibility to write there (like file access rights or a read-only drive).

Upvotes: 1

Related Questions