0

I am trying to learn C. :) however I am getting this error which I do not understand . Can somebody please explain this to me. I am not able to understand what is causing this issue.

#include<stdio.h>
#include<conio.h>

main()
{
    FILE *fp, *ft;
    char ch;
    fp=fopen("D:\Documents\sample.txt","w");
    if (fp ==NULL) 
    {
        puts("cannot open file");
        exit();
    }

    ft=fopen("D:\Documents\sample - Copy.txt","w");
     if (ft ==NULL) 
    {
        puts("cannot open file");
        exit();
    }

    while(1)
    {
        ch=fgetc(fp);
        if (ch== EOF)
            break;
        else
            fputc(ch,ft);
    }
    fclose(fp);
    fclose(ft);
}

Error message I am getting :-

C:\Users\LoneRanger\Desktop\FileHandling.c: In function 'main':
C:\Users\LoneRanger\Desktop\FileHandling.c:8:11: warning: unknown escape sequenc
e: '\D' [enabled by default]
  fp=fopen("D:\Documents\sample.txt","w");
           ^
C:\Users\LoneRanger\Desktop\FileHandling.c:8:11: warning: unknown escape sequenc
e: '\s' [enabled by default]
C:\Users\LoneRanger\Desktop\FileHandling.c:12:6: warning: incompatible implicit
declaration of built-in function 'exit' [enabled by default]
      exit();
      ^
C:\Users\LoneRanger\Desktop\FileHandling.c:12:6: error: too few arguments to fun
ction 'exit'
C:\Users\LoneRanger\Desktop\FileHandling.c:15:11: warning: unknown escape sequen
ce: '\D' [enabled by default]
  ft=fopen("D:\Documents\sample - Copy.txt","w");
           ^
C:\Users\LoneRanger\Desktop\FileHandling.c:15:11: warning: unknown escape sequen
ce: '\s' [enabled by default]
C:\Users\LoneRanger\Desktop\FileHandling.c:19:6: warning: incompatible implicit
declaration of built-in function 'exit' [enabled by default]
      exit();
      ^
C:\Users\LoneRanger\Desktop\FileHandling.c:19:6: error: too few arguments to fun
ction 'exit'

C:\Users\LoneRanger\Desktop>gcc C:\Users\LoneRanger\Desktop\FileHandling.c -o Fi
leHandling
C:\Users\LoneRanger\Desktop\FileHandling.c: In function 'main':
C:\Users\LoneRanger\Desktop\FileHandling.c:8:11: warning: unknown escape sequenc
e: '\D' [enabled by default]
  fp=fopen("D:/\Documents/\sample.txt","w");
           ^
C:\Users\LoneRanger\Desktop\FileHandling.c:8:11: warning: unknown escape sequenc
e: '\s' [enabled by default]
C:\Users\LoneRanger\Desktop\FileHandling.c:12:6: warning: incompatible implicit
declaration of built-in function 'exit' [enabled by default]
      exit();
      ^
C:\Users\LoneRanger\Desktop\FileHandling.c:12:6: error: too few arguments to fun
ction 'exit'
C:\Users\LoneRanger\Desktop\FileHandling.c:15:11: warning: unknown escape sequen
ce: '\D' [enabled by default]
  ft=fopen("D:/\Documents/\sample - Copy.txt","w");
           ^
C:\Users\LoneRanger\Desktop\FileHandling.c:15:11: warning: unknown escape sequen
ce: '\s' [enabled by default]
C:\Users\LoneRanger\Desktop\FileHandling.c:19:6: warning: incompatible implicit
declaration of built-in function 'exit' [enabled by default]
      exit();
      ^
C:\Users\LoneRanger\Desktop\FileHandling.c:19:6: error: too few arguments to fun
ction 'exit'
2
  • 1
    C is not C++ or Objective-C. Commented Mar 18, 2015 at 17:18
  • It says "error: too few arguments to function 'exit'". Change exit() to exit(1) for example. Commented Mar 18, 2015 at 17:24

3 Answers 3

2

\ is escape symbol in C, you should use \\ in your strings, like:

fp=fopen("D:\\Documents\\sample.txt","w");
Sign up to request clarification or add additional context in comments.

Comments

2

exit() is in stdlib.h and you don't include this header so there is a error.

Also return is a better option than exit here

Comments

1

In C, C++, C#, and a bunch of other languages, you can make strings with special characters, such as adding a new line ('\n'), a tab ('\t') and many others.

The convention is to 'escape' them, that is, use a backslash \ and one or more characters to symbolize the character you want. This means that \ is special in a string and you need to use it with care.

In your case, when you want to use a real \ to separate directories, you need to escape it as \\

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.