如何获得排除空终止符字符串中读取时? [英] How to get exclude null terminators when reading in a string?

查看:210
本文介绍了如何获得排除空终止符字符串中读取时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本程序打开一个包含一个湖的名字和它在数百英里立方的单位体积的文件 - 用空格隔开。它的输出被认为是湖的名字后面跟一个数字的星号来重新present其体积至最接近的百立方英里(例如,有12.7百立方英里体积湖将打印13星号)。然而,当它在包含空间的名称读取时,它读取直到空间,然后打印在新的行中的下一个字符串。有什么办法,我可以读脏毛湖为一条线,而不是毛\\ ndirty \\ nlake的例子?这是我到目前为止有:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&math.h中GT;无效name_asterisks(焦炭名[20],浮动体积);主(){
       FILE * FP;
       FP = FOPEN(lakes.txt,R);
       焦炭名[20];
       浮卷;
       如果(FP == NULL){
          的printf(文件不存在\\ n);
          系统(暂停);
          返回0;
       }
       而(的fscanf(FP,%s%F,名称和放大器;!体积)= EOF){
             name_asterisks(姓名,体积);
       }
       FCLOSE(FP);
       系统(暂停);
}无效name_asterisks(焦炭名[20],浮动体积){
     INT I;
     的printf(%S,名);
     对于(I = 0; I&≤(INT)roundf(体积);我+ +)
         的printf(*);
     的printf(\\ n);
}


解决方案

%S是用于扫描非空白。 code需要一个不同的格式说明。

 字符BUF [100];
而(与fgets(buf中,sizeof的BUF,FP)!= NULL){
   如果(的sscanf(buf中,%19 [A-ZA-Z]%F,名称和放大器;!体积)= 2){
     fprintf中(标准错误,意外的数据\\ n);
     打破;
   }
   name_asterisks(姓名,体积);
 }

:跳过的空格结果。
%19 [A-ZA-Z]:扫描并保存多达19字母或空格,追加'\\ 0'。结果
%F:跳过空白字符和保存扫描浮动

备注原来的code:最好检查什么code要针对比1意外结果检查

  //而(的fscanf(FP,%s%F,名称和放大器;!体积)= EOF){
而(的fscanf(FP,%s%F,名称与放;体积)== 2){

This program opens a file that contains a lake's name and its volume in units of hundreds of cubic miles--separated by a space. Its output is supposed to be the lake's name followed by a number of asterisks to represent its volume to the nearest hundred cubic mile (for example, a lake that has 12.7 hundred cubic miles in volume would print 13 asterisks). However, when it reads in a name that contains a space, it reads up until the space and then prints the next string in a new line. Is there any way I can read "gross dirty lake" as one line instead of "gross\ndirty\nlake" for example? Here's what I have so far:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void name_asterisks(char name[20], float vol);

main() {
       FILE *fp;
       fp = fopen("lakes.txt", "r");
       char name[20];
       float vol;
       if (fp == NULL) {
          printf("File does not exist.\n");
          system("pause");
          return 0;
       }
       while (fscanf(fp, "%s %f", name, &vol) != EOF) {
             name_asterisks(name, vol);
       }
       fclose(fp);
       system("pause");
}

void name_asterisks(char name[20], float vol) {
     int i;
     printf("%s", name);
     for (i = 0; i < (int)roundf(vol); i++)
         printf("*");
     printf("\n");
}

解决方案

"%s" is for scanning non-white-space. Code needs a different format specifier.

char buf[100];
while (fgets(buf, sizeof buf, fp) != NULL) {
   if (sscanf(buf, " %19[A-Za-z ]%f", name, &vol) != 2) {
     fprintf(stderr, "Unexpected data\n");
     break;
   }
   name_asterisks(name, vol);
 }

" ": Skip white-spaces.
"%19[A-Za-z ]": Scan and save up to 19 letters or spaces, append '\0'.
"%f": Skip white-spaces and save scan a float.

Note about original code: Better to check for what code wants than checking against 1 undesired result

// while (fscanf(fp, "%s %f", name, &vol) != EOF) {
while (fscanf(fp, "%s %f", name, &vol) == 2) {

这篇关于如何获得排除空终止符字符串中读取时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆