c/c++语言开发共享用fscanf或fgets忽略空格?

我想知道是否有任何方法可以忽略fscanf或fgets函数的空格。 我的文本文件在每一行上有两个字符,可以用空格分隔,也可以不用空格分隔。 我需要读取两个字符并将每个字符放在不同的数组中。

file = fopen(argv[1], "r"); if ((file = fopen(argv[1], "r")) == NULL) { printf("nError opening file"); } while (fscanf(file, "%s", str) != EOF) { printf("n%s", str); vertexArray[i].label = str[0]; dirc[i] = str[1]; i += 1; } 

    使用fscanf格式的空格( " " )会使其读取并丢弃输入上的空格,直到找到非空白字符,并将输入上的非空白字符作为要读取的下一个字符。 所以你可以这样做:

     fscanf(file, " "); // skip whitespace getc(file); // get the non-whitespace character fscanf(file, " "); // skip whitespace getc(file); // get the non-whitespace character 

    要么

     fscanf(file, " %c %c", &char1, &char2); // read 2 non-whitespace characters, skipping any whitespace before each 

      以上就是c/c++开发分享用fscanf或fgets忽略空格?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/553085.html

      (0)
      上一篇 2021年1月26日
      下一篇 2021年1月26日

      精彩推荐