如何在Mac上ifstream CodeBlocks
How to Ifstream codeblocks on mac?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("test.txt");
int foo;
string sFoo;
inFile >> sFoo;
inFile >> foo;
cout << "the name is " << sFoo << endl;
cout << "the first number is " << foo << endl;
inFile >> foo;
cout << "the second number is " << foo << endl;
cout << "Hello World!";
return 0;
}
我尝试将文本文件放在同一文件夹中。但是,由于某种原因,它无法读取文本文件。请有人告诉我在MacBook上的CodeBlocks中该怎么做!
您必须编写文件的完整绝对路径,而不是相对。我在这里回答了同样的问题。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("/Users/user/Desktop/test.txt");
if(inFile){
int foo;
string sFoo;
inFile >> sFoo;
inFile >> foo;
cout << "the name is " << sFoo << endl;
cout << "the first number is " << foo << endl;
inFile >> foo;
cout << "the second number is " << foo << endl;
cout << "Hello World!";
inFile.close();
}else{
cout<<"unable to open file"<<endl;
}
return 0;
}
相关文章:
- ifstream什么都没读
- ifstream文件在从行中读取时被清除
- 为什么这种情况发生在CodeBlocks上?
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- 将 ifstream 拆分为 n 个流?
- 如何将 std::ifstream 转换为 std::basic_istream<CharT, Traits>&?
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 是否可以在没有真实文件的情况下创建 ifstream
- 虽然循环不读取整个 ifstream 文件
- fopen_s 未在 scope(C++:codeblocks ) 中声明
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- Ofstream和IFSTREAM有什么区别?
- 如何将QByteArray转换为std::istream或std::ifstream?
- ifstream 跳过数字,只写 0
- C++:std::ofstream 方法 open() 在第二次迭代时擦除打开的 ifstream 文件
- 在函数中声明的 ifStream 对象在发布模式下不起作用
- fstream / ifstream / ofstream 对象如何转换为布尔值
- ifstream 尝试读取 9 到 13 之间的无符号字符时非常奇怪的行为
- 如何在Mac上ifstream CodeBlocks