小mi粥的米 2025-01-07 10:20 采纳率: 72.7%
浏览 19

CCS打开txt文件失败,什么原因?

console显示"File open fail!,进入readfile函数就会 自动跳转到string.h文件的这个while循环里。 检查了文件路径和文件内容都没有问题 ,什么原因?

double* read_file(double* data)
{
    int dig[1000] = { 0 }; // 小数位数
    int count,x;
    count = 0;
    x = 0;
    FILE* fp;
    fp = fopen("Data_10_2_temp.txt", "r");

    if (!fp)
    {
        printf("File open fail!\n");
        //return NULL;
    }

    char* buffer = (char*)malloc(CH_SIZE * sizeof(char));
    if (buffer == NULL)
    {
        printf("缓存分配失败\n");
        fclose(fp);
        return NULL;
    }

    memset(buffer, 0, CH_SIZE * sizeof(char));
    while (1)
    {
        if (fscanf(fp, "%s", buffer) == 1)
        {
            if (sscanf(buffer, "%lf", &data[count] ) == 1)
            {
                char* ptr2 = strchr(buffer, 'e');
                if (ptr2)
                { // 如果是科学计数法,提取出整数部分,根据符号位计算小数位
                    ptr2++;
                    sscanf(ptr2, "%d", &x);
                    if (*ptr2 == '-')
                        x += 4;
                    else
                        x += 3;
                }
                else
                    x = 0;
                dig[count] = (int)(strlen(buffer) - 4 - x);
                if (dig[count] <= 0)
                    dig[count] = 1; // 如果是 1 位或 2 位的整数,输出 1 位小数位
                 count++;
                if (count > 979)
                    break;
            }
            memset(buffer, 0, CH_SIZE * sizeof(char));
        }
        else
            break;
    }

    fclose(fp);
    free(buffer);

    return data;
}

进入readfile函数就会自动 跳转到string.h文件的memset这句while循环里一直跑。

img

  • 写回答

1条回答 默认 最新

  • 檀越@新空间 2025-01-07 10:25
    关注

    上午好☀️☀️☀️️
    本答案参考通义千问

    根据你的描述和提供的代码,CCS 打开 txt 文件失败的原因可能有多种。以下是一些可能的原因和相应的解决方法:

    1. 文件路径错误

      • 确保文件路径是正确的,并且文件存在于指定路径中。
      • 可以尝试使用绝对路径来排除相对路径的问题。
    2. 权限问题

      • 确保程序有足够的权限去读取该文件。
    3. 文件已被其他进程占用

      • 确保没有其他程序正在使用该文件。
    4. 文件格式问题

      • 确保文件内容符合预期格式(例如,每行一个数字)。
    5. 编译器/IDE 配置问题

      • 确保编译器和 IDE 配置正确,能够找到并打开文件。
    6. 缓冲区大小问题

      • 确保 CH_SIZE 定义合理,且足够大以容纳文件中的数据。

    解决方案

    1. 检查文件路径

      • 使用绝对路径来确保路径正确。
      fp = fopen("/absolute/path/to/Data_10_2_temp.txt", "r");
      
    2. 添加错误处理

      • 添加更多详细的错误信息以便更好地诊断问题。
      if (!fp)
      {
          printf("File open fail! Path: %s\n", "Data_10_2_temp.txt");
          perror("Error details:");
          return NULL;
      }
      
    3. 调试输出

      • 在关键位置添加调试输出以了解程序执行过程。
      printf("Attempting to open file: %s\n", "Data_10_2_temp.txt");
      fp = fopen("Data_10_2_temp.txt", "r");
      if (!fp)
      {
          printf("File open fail! Path: %s\n", "Data_10_2_temp.txt");
          perror("Error details:");
          return NULL;
      }
      
    4. 检查文件权限

      • 确保文件具有可读权限。
      chmod +r Data_10_2_temp.txt
      
    5. 检查文件内容格式

      • 确保文件内容格式正确,每行一个浮点数。
      • 示例文件内容:
        1.23
        4.56
        7.89
        
    6. 确保 CH_SIZE 合理

      • 根据文件大小调整 CH_SIZE
      #define CH_SIZE 1024
      

    修改后的代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define CH_SIZE 1024
    
    double* read_file(double* data)
    {
        int dig[1000] = { 0 }; // 小数位数
        int count, x;
        count = 0;
        x = 0;
        FILE* fp;
        fp = fopen("Data_10_2_temp.txt", "r");
    
        if (!fp)
        {
            printf("File open fail! Path: %s\n", "Data_10_2_temp.txt");
            perror("Error details:");
            return NULL;
        }
    
        char* buffer = (char*)malloc(CH_SIZE * sizeof(char));
        if (buffer == NULL)
        {
            printf("缓存分配失败\n");
            fclose(fp);
            return NULL;
        }
    
        memset(buffer, 0, CH_SIZE * sizeof(char));
        while (1)
        {
            if (fscanf(fp, "%s", buffer) == 1)
            {
                if (sscanf(buffer, "%lf", &data[count]) == 1)
                {
                    char* ptr2 = strchr(buffer, 'e');
                    if (ptr2)
                    { // 如果是科学计数法,提取出整数部分,根据符号位计算小数位
                        ptr2++;
                        sscanf(ptr2, "%d", &x);
                        if (*ptr2 == '-')
                            x += 4;
                        else
                            x += 3;
                    }
                    else
                        x = 0;
                    dig[count] = (int)(strlen(buffer) - 4 - x);
                    if (dig[count] <= 0)
                        dig[count] = 1; // 如果是 1 位或 2 位的整数,输出 1 位小数位
                    count++;
                    if (count > 979)
                        break;
                }
                memset(buffer, 0, CH_SIZE * sizeof(char));
            }
            else
                break;
        }
    
        fclose(fp);
        free(buffer);
    
        return data;
    }
    

    通过这些步骤,你可以更好地诊断和解决问题。希望这些建议能帮助你解决文件打开失败的问题。

    评论

报告相同问题?

问题事件

  • 修改了问题 1月7日
  • 创建了问题 1月7日