【问题标题】:Read a line of strings from a file从文件中读取一行字符串
【发布时间】:2018-04-21 18:53:05
【问题描述】:

我想从文件中读取一整行字符串以执行一些操作。使用下面的代码,“密文”为空。

我的代码:

#include<iostream>
#include<string.h>
#include<stdlib.h>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char **argv)
{
    ifstream myfile("C:\\encr_affine.txt");
    string Ciphertext;

    while (!myfile.eof())
    {
        getline(myfile, Ciphertext);
    }

    //some code here

myfile.close();

}

【问题讨论】:

  • 请删掉问题中所有无用的空行,因为它们会使这段代码无缘无故地滚动,并使其更难阅读。
  • @FeiXiang 谢谢 :) 如果你想把它写成答案...
  • 我不确定这是您代码中的唯一问题。无论如何,此类调试问题通常都不会被很好地接受,因为您可能已经发现问题出在使用调试器的循环语句中,然后可能会查找并找到我链接到的帖子。 (虽然我没有投反对票)

标签: c++ file-io


【解决方案1】:

读取文件中一行字符串的答案是:

while (getline(myfile, Ciphertext))
{
            //reading the ciphertext from the file
}

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多