鱼C论坛

 找回密码
 立即注册
查看: 1446|回复: 7

[已解决]请问为什么这个C里面的fileI/O没法实现

[复制链接]
发表于 2020-11-4 11:49:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一个较为简单的读取一个txt文档中的每一行
代码如下,但每次输入时总会显示“cannot open file" 而且我已经把commands.txt放进程序同一目录下,却还是如此,请大佬指正。
#include <stdio.h>
#include <string.h>

int main ()
{
    FILE *fp;
    int n = 0;
    char expression[256];

    // open the file
    if (!(fp = fopen("commands.txt","rt")))
    {
        printf("cannot open file!\n");
        return -1;
    }
    // read the file line by line
    do{
        if(fgets(expression, 256, fp))
        {
            // remove the '\n' if it exists
            if (expression[strlen(expression)-1] == '\n')
                expression[strlen(expression)-1] = '\0';
            // do whatever you want for the line
            printf("line %d: %s\n", ++n, expression);
        }
    }
    while(!feof(fp));
    fclose(fp);
    return 0;
}
最佳答案
2020-11-4 13:29:22
jpan1221 发表于 2020-11-4 12:10
好像还是不可以诶 还是“cannot open file!”

这样,你将 commands.txt 文件放到C盘根目录下,试一下我这个程序:


  1. #include <stdio.h>
  2. #include <string.h>

  3. int main ()
  4. {
  5.     FILE *fp;
  6.     int n = 0;
  7.     char expression[256];

  8.     // open the file
  9.     if((fp=fopen("c:\\commands.txt","rt"))==NULL)
  10.     {
  11.         printf("cannot open file!\n");
  12.         return -1;
  13.     }
  14.     // read the file line by line
  15.     do{
  16.         if(fgets(expression, 256, fp))
  17.         {
  18.             // remove the '\n' if it exists
  19.             if (expression[strlen(expression)-1] == '\n')
  20.                 expression[strlen(expression)-1] = '\0';
  21.             // do whatever you want for the line
  22.             printf("line %d: %s\n", ++n, expression);
  23.         }
  24.     }
  25.     while(!feof(fp));
  26.     fclose(fp);
  27.     return 0;
  28. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-4 12:03:55 | 显示全部楼层
将 if (!(fp = fopen("commands.txt","rt"))) 改为:
    if((fp=fopen("commands.txt","rt"))==NULL) 试一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-4 12:10:56 | 显示全部楼层
风过无痕1989 发表于 2020-11-4 12:03
将 if (!(fp = fopen("commands.txt","rt"))) 改为:
    if((fp=fopen("commands.txt","rt"))==NULL) 试 ...

好像还是不可以诶 还是“cannot open file!”
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-4 12:43:49 | 显示全部楼层
        这一句:
  1.         if (!(fp = fopen("commands.txt","rt")))
复制代码

        一定是错的!必须照 2 楼说的改,改过以后代码就没有问题了,如果再说找不着文件,那问题就是,文件一定不存在。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-4 13:25:56 | 显示全部楼层
jackz007 发表于 2020-11-4 12:43
这一句:

        一定是错的!必须照 2 楼说的改,改过以后代码就没有问题了,如果再说找不着 ...

如图
YWM{3HY`)Y]JR%~ZV}`K[@8.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-4 13:29:22 | 显示全部楼层    本楼为最佳答案   
jpan1221 发表于 2020-11-4 12:10
好像还是不可以诶 还是“cannot open file!”

这样,你将 commands.txt 文件放到C盘根目录下,试一下我这个程序:


  1. #include <stdio.h>
  2. #include <string.h>

  3. int main ()
  4. {
  5.     FILE *fp;
  6.     int n = 0;
  7.     char expression[256];

  8.     // open the file
  9.     if((fp=fopen("c:\\commands.txt","rt"))==NULL)
  10.     {
  11.         printf("cannot open file!\n");
  12.         return -1;
  13.     }
  14.     // read the file line by line
  15.     do{
  16.         if(fgets(expression, 256, fp))
  17.         {
  18.             // remove the '\n' if it exists
  19.             if (expression[strlen(expression)-1] == '\n')
  20.                 expression[strlen(expression)-1] = '\0';
  21.             // do whatever you want for the line
  22.             printf("line %d: %s\n", ++n, expression);
  23.         }
  24.     }
  25.     while(!feof(fp));
  26.     fclose(fp);
  27.     return 0;
  28. }
复制代码

打开文件.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-4 13:37:07 | 显示全部楼层

你5楼发的图片中,if((fp=open(_filename: "commands.txt", _Mode: "rt"))==NULL) 多了红色部份的东西,将它们去掉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-4 14:02:48 | 显示全部楼层
风过无痕1989 发表于 2020-11-4 13:37
你5楼发的图片中,if((fp=open(_filename: "commands.txt", _Mode: "rt"))==NULL) 多了红色部份的东西, ...

嗯,可以了谢谢 红色部分应该是clion编译器自动附上的注释,去不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-11 15:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表