如何读取一个字符串从\\ n分隔的文件 [英] how to read a string from a \n delimited file

查看:108
本文介绍了如何读取一个字符串从\\ n分隔的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读一回分隔的文件。全短语。

I'm trying to read a return delimited file. full of phrases.

我试图把每个短语转换成字符串。

I'm trying to put each phrase into a string.

问题是,当我尝试读取文件

The problem is that when I try to read the file with

fscanf(file,"%50s\n",string);

该字符串只包含一个字。当它用空格颠簸它停止读取字符串

the string only contains one word. when it bumps with a space it stops reading the string

推荐答案

的fscanf可以修改为过去的空间。细节是有点复杂。下面是手册页说,关于什么样%[...]

fscanf can be modified to read past spaces. The details are a bit complicated. Here is what the man page says about %[...]

匹配的非空子序列
  从指定的一组字符
  接受的字符;接下来的指针
  必须为char指针,有
  必须有足够的空间的所有
  字符串中的字符,再加上
  终止NUL字符。通常
  跳过前面的空格是
  SUP pressed。该字符串是要制成
  向上(或不在)炭acters的
  特别是集;该组被定义为
  开放之间的字符
  括号[字符和一个亲密
  支架]字符。该组不包括
  如果第一这些字符
  字符之后的左括号是
  抑扬^。为包含一个关闭
  支架的设置,使其成为第一
  开括号后的字符或
  回旋;任何其他职位
  将结束集中。连字符
  字符 - 也特别;什么时候
  放在其他两个字符之间,
  它增加了所有介入的字符
  集合。要包含一个连字符,使它
  决赛前的最后一个字符
  关闭支架。例如,
  `[^] 0-9-]是指一组``一切
  除了通过密切支架,零
  九,和连'。字符串结束
  与字符的外观不
  在(或者用一个音调,在)集
  或者当字段宽度耗尽。

Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating NUL character. The usual skip of leading white space is suppressed. The string is to be made up of char-acters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] character. The set excludes those characters if the first character after the open bracket is a circumflex ^. To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, `[^]0-9-]' means the set ``everything except close bracket, zero through nine, and hyphen''. The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out.

所以,%[^ \\ n]的应阅读一切都交给回车。

So, %[^\n] should read everything up to the carriage return.

这篇关于如何读取一个字符串从\\ n分隔的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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