在C中从文本文件读取数据 [英] Reading data from text file in C

查看:89
本文介绍了在C中从文本文件读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其中包含用空格分隔的单词.我想从文件中提取每个单词并将其存储.所以我打开了文件,但不确定如何将单词分配给char.

I have a text file which contains words separated by space. I want to take each word from the file and store it. So i have opened the file but am unsure how to assign the word to a char.

FILE *fp;
fp = fopen("file.txt", "r");
//then i want
char one = the first word in the file
char two = the second word in the file

FILE *fp;
fp = fopen("file.txt", "r");
//then i want
char one = the first word in the file
char two = the second word in the file

推荐答案

您不能将一个单词分配给char.您可以将一个字符分配给char-从而命名.您可以将一个单词分配给字符数组-例如s [128].

You cannot assign a word to a char. You can assign a single character to a char - hence the name. You can assign a word to an array of characters - such as s[128].

例如:

     char word[128];
     fscanf(fp, "%s", word);

请注意,在生产代码中,您不能仅使用静态大小的缓冲区,这将导致缓冲区溢出可利用代码.

Note, in production code you cannot just use statically sized buffer, this will lead to buffer overflow exploitable code.

这篇关于在C中从文本文件读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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