用fscanf读取字符串
Reading a string with fscanf
我想知道如何使用fscanf从文件(称为myfile)读取字符串。我写了这篇文章:
FILE *myFile;
string name[100];
int grade, t = 0, place = 0;
if (myFile == NULL) {
cout << "File not found";
return;
}
while (t != EOF) {
t = fscanf(myFile, "%s %dn", &name[place], &grade[place]);
place++;
}
它给了我这个错误:
错误c2109下标要求使用FSCANF在行上的数组或指针类型我已经使用了iostream和stdio.h
等级是int,您不需要索引。
t = fscanf(myFile, "%s %dn", &name[place], &grade[place]);
应该是
t = fscanf(myFile, "%s %dn", &name[place], &grade);
在C 中,您可以使用:
#include <fstream>
std::ifstream file("myFile.txt");
假设文件的每一行是一个字符串,然后是int,如您的代码中,您可以使用类似的内容:
#include <iostream>
#include <fstream>
int main(){
int place =0,grade[5];
std::string name[5];
std::ifstream file("myFile.txt");
while(!file.eof()){ // end of file
file >>name[place]>>grade[place];
place++;
}
return 0;
//Make sure you check the sizes of the buffers and if there was no error
//at the opening of the file
}
相关文章:
- 如何使用Luacneneneba API正确读取字符串和表参数
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 如何从字符串C++读取多个整数
- 使用字符串读取的字符串
- 从文件到字符串读取并传递到功能
- C++ cin 将字符串读取到 int 类型中返回 0
- 使用CIN和/或字符串读取带有混合输入的文本文件
- 将两个字符串作为一个字符串读取
- 如何将WCP 1252字符串读取为std::tstring
- 如何将字符串读取为字符串向量或将整数读取为整数向量
- 将一系列字符串读取到char数组中
- 如何在 c++ 中将文件中的段落作为单个字符串读取
- 从 txt 文件中将后缀表达式作为字符串读取
- C++银行项目;如何将一组字符串读取为单独的浮点数并添加它们
- 在 c++ 中循环字符串读取器
- 将字符串读取为命令行参数C++
- 如何在QT中使用QDomDocument对象将字符串读取为XML
- 使用Matio库将.mat文件中的字符串读取到C++中
- 作为字符串读取第一列数据文件
- 如何在字符串读取完成时停止c++的recv() ?