简体   繁体   中英

File Handling in C programming

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'

\\是C中的转义符号,您应该在字符串中使用\\\\ ,例如:

fp=fopen("D:\\Documents\\sample.txt","w");

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

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 \\\\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM