m0_62267751 2021-12-01 23:59 采纳率: 85.7%
浏览 25
已结题

C语言 文件 用法 不知道这个错误是什么意思 想读取一个csv文件 文件也保存了


#include<stdio.h>
#include<string.h>
int main(){
FILE *fp = NULL;
    char *line,*record;
    char buffer[1024];
    if((fp = fopen("c:\\menu.csv", "r")) != NULL)
    {
        fseek(fp, 0L, SEEK_SET);  //定位到第二行,每个英文字符大小为1
        char delims[] = ",";
        char *result = NULL;
        int j = 0;
        while ((line = fgets(buffer, sizeof(buffer), fp))!=NULL)//当没有读取到文件末尾时循环继续
        {
            record = strtok(line, ",");
            while (record != NULL)//读取每一行的数据
            {
                if (strcmp(record, "Ps") == 0)//当读取到Ps那一行时,不再继续读取
                    return 0;
                printf("%s ", record);//将读取到的每一个数据打印出来
                if (j == 3)  //只需读取前9列
                    break;
                record = strtok(NULL, ",");
                j++;
            }
            printf("\n");
            j = 0;
 
        }
        fclose(fp);
        fp = NULL;
    }

}

img


是不是不能在vscode上面读出表格啊?

  • 写回答

2条回答 默认 最新

  • 赵4老师 2021-12-02 09:37
    关注

    record = strtok(buffer, ",");

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 赵4老师 2021-12-02 18:07
    关注

    刚又查了一下:

    fgets, fgetws
    Get a string from a stream.
    
    char *fgets( char *string, int n, FILE *stream );
    
    wchar_t *fgetws( wchar_t *string, int n, FILE *stream );
    
    Function Required Header Compatibility 
    fgets <stdio.h> ANSI, Win 95, Win NT 
    fgetws <stdio.h> or <wchar.h> ANSI, Win 95, Win NT 
    
    
    For additional compatibility information, see Compatibility in the Introduction.
    
    Libraries
    
    LIBC.LIB Single thread static library, retail version 
    LIBCMT.LIB Multithread static library, retail version 
    MSVCRT.LIB Import library for MSVCRT.DLL, retail version 
    
    
    Return Value
    
    Each of these functions returns string. NULL is returned to indicate an error or an end-of-file condition. Use feof or ferror to determine whether an error occurred.
    
    Parameters
    
    string
    
    Storage location for data
    
    n
    
    Maximum number of characters to read
    
    stream
    
    Pointer to FILE structure
    
    Remarks
    
    The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string. 
    
    fgets is similar to the gets function; however, gets replaces the newline character with NULL. fgetws is a wide-character version of fgets. 
    
    fgetws reads the wide-character argument string as a multibyte-character string or a wide-character string according to whether stream is opened in text mode or binary mode, respectively. For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.
    
    Generic-Text Routine Mappings
    
    TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined 
    _fgetts fgets fgets fgetws 
    
    
    Example
    
    /* FGETS.C: This program uses fgets to display
     * a line from a file on the screen.
     */
    
    #include <stdio.h>
    
    void main( void )
    {
       FILE *stream;
       char line[100];
    
       if( (stream = fopen( "fgets.c", "r" )) != NULL )
       {
          if( fgets( line, 100, stream ) == NULL)
             printf( "fgets error\n" );
          else
             printf( "%s", line);
          fclose( stream );
       }
    }
    
    
    Output
    
    /* FGETS.C: This program uses fgets to display
     
    
    
    Stream I/O Routines
    
    See Also   fputs, gets, puts
    
    
    
    

    我之前的回复
    record = strtok(buffer, ",");
    改不改是一回事。

    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月10日
  • 已采纳回答 12月2日
  • 修改了问题 12月2日
  • 修改了问题 12月1日
  • 展开全部

悬赏问题

  • ¥50 burgers方程求逆
  • ¥15 matlab最小二乘法拟合非线性曲线提问
  • ¥15 电脑锁屏时vba运行SAP,excel数据不能复制到SAP
  • ¥50 74LS系列 74LS00 74LS04设计一个RS485电路(关键词-差分)
  • ¥30 各位help写一下代码
  • ¥15 在运行SDEdit模型下载不了
  • ¥15 求51控制l298n驱动的小车中超声波避障怎么写
  • ¥15 电脑连上WIFI却用不了
  • ¥30 MATLAB在RLC电路的固有响应和阶跃响应GUI仿真报告
  • ¥15 hyper-v出现的问题