在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/C  Android/ C語言請問如何 讀取一個文件中的字符?

C語言請問如何 讀取一個文件中的字符?

用c打開一個TXT文件并讀取里面的字符 在讀寫指定文件時出現(xiàn)錯誤 請問該如何解決?

clipboard.png

include<stdio.h>

include<stdlib.h>

int main(void)
{

FILE *fp;
char str[3][10];
int i = 0;
if ((fp = fopen_s("C:\Users\ASUS\Desktop.txt", "r",10)) == NULL)
{
    printf("can't open file!\n");
    exit(0);
}
while (fgets(str[i], 10, fp) != NULL)
{
    printf("%s", str[i]);
    i++;
}
fclose(fp);
return 0;

}

回答
編輯回答
孤影

函數(shù)用錯了,你把 fopen_s 當成 fopen 在用了,它們的參數(shù)并不相同。

fopen_s 示例(僅供參考)

// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-s-wfopen-s
// errno_t fopen_s(FILE** pFile, const char *filename, const char *mode);

#include <stdio.h>

int main( void )
{
   FILE *stream;
   errno_t err;
   // Open for read (will fail if file "crt_fopen_s.c" does not exist)
   err  = fopen_s( &stream, "crt_fopen_s.c", "r" );
   if( err == 0 )
      printf( "The file 'crt_fopen_s.c' was opened\n" );
   else
      printf( "The file 'crt_fopen_s.c' was not opened\n" );
}

fopen 示例(僅供參考)

// http://www.cplusplus.com/reference/cstdio/fopen/
// FILE * fopen ( const char * filename, const char * mode );

#include <stdio.h>
int main ()
{
  FILE * pFile;
  pFile = fopen ("myfile.txt","w");
  if (pFile!=NULL)
  {
    fputs ("fopen example",pFile);
    fclose (pFile);
  }
  return 0;
}
2017年8月28日 08:17