简体   繁体   English

文件未显示在cmd提示窗口上

[英]File not displaying on cmd prompt window

I am fairly new to c++. 我是C ++的新手。 I am takking a class on intro to structures and objects right now. 我现在正在讲授有关结构和对象的入门课程。 We finnaly went over files and I got the idea to write a program that encrypts a text file. 我们最终检查了文件,然后想到了编写一个加密文本文件的程序的想法。 Just for my own pleasure and knowledge (this is not homework). 仅出于我自己的乐趣和知识(这不是家庭作业)。 I havent written the encryption yet, it will be private key because I was told that was easiest and this is my first time trying something like this. 我还没有编写加密,它将是私钥,因为有人告诉我这是最简单的,这是我第一次尝试这样的事情。 Anyway I am just writing the code as functions right now to make sure they work, then will make them into classes later on and expand on the program. 无论如何,我现在只是将代码作为函数编写,以确保它们能正常工作,然后稍后将它们分成类并在程序上进行扩展。 So, right now my code will open and write a file. 因此,现在我的代码将打开并写入文件。 I want to view the file I just wrote in the cmd window before encrypting so I know that I can see the before and after. 我想在加密之前查看我刚在cmd窗口中写入的文件,因此我知道可以看到之前和之后的内容。 Heres the code: 这是代码:

//This program will create, store and encrypt a file for sending over the inernet

#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<string>
#include<fstream>
#include"fileSelect.h"

using namespace std;

void openFile(fstream &);
void readFile(fstream &);

int main() {
    //output file stream
    fstream outputStream;
    fstream inputStream;
    string fileName, line;

    openFile(outputStream);
    readFile(inputStream);

    system("pause");
    return 0;
}
//open file Def
void openFile(fstream &fout){
    string fileName;
    char ch;
    cout<<"Enter the name of the file to open: ";
    getline(cin, fileName);
    //try to open the file for writing
    fout.open(fileName.c_str(),ios::out);
    if(fout.fail()){
        cout<<"File, "<<fileName<<" failed to open.\n";
        exit(1);
    }
    cout<<"Enter your message to encrypt. End message with '&':\n";
    cin.get(ch);
    while(ch!='.'){
        fout.put(ch);
        cin.get(ch);
    }
    fout.close();
    return;
}

void readFile(fstream &fin){
    string fileName, line;
    cout<<endl;
    cout<<"Enter the name of the file to open: ";
    getline(cin, fileName);
    //check file is good
    if(fin.fail()){
        cout<<"File "<<fileName<<" failed to open.\n";
        exit(1);
    }

    cout<<"Opening "<<"'"<<fileName<<"'" <<endl;
    //cout<<"Enter the file to open: ";
    //cin>>fileName;
    fin.open(fileName.c_str(),ios::in);
    //readFile(inputStream);
    if(fin){
        //read in data
        getline(fin,line);
        while(fin){
            cout<<line<<endl;
            getline(fin,line);
        }

        fin.close();
    }
    else{
        cout<<"Error displaying file.\n";
    }

    return ;
}

This will compile and run. 这将编译并运行。 If openFile() is commented out and the readFile() function is called by itself, the file will read what was written in openFile(). 如果openFile()被注释掉并且readFile()函数本身被调用,则文件将读取openFile()中写入的内容。 It just wont do it one after the other like I want. 它只是不会像我想要的那样一个接一个地做。 It is probably just a simple fix that I am missing but its becoming a bit of a headache now. 这可能只是我遗漏的简单解决方法,但现在变得有些头疼。 Any help would be apreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

When you read the message to encrypt, you don't consume the newline. 阅读要加密的消息时,您不会占用换行符。 The newline remains in the input buffer and will cause getline(cin, fileName); 换行符保留在输入缓冲区中,并将导致getline(cin, fileName); to read an empty fileName . 读取一个空的fileName

You must first skip the newline after reading the message 阅读消息后,您必须先跳过换行符

string tmp;
getline(cin, tmp);

and then the getline() in readFile will work properly. 然后readFilegetline()将正常工作。

OT : OT

  • Your prompt says 您的提示说

     cout<<"Enter your message to encrypt. End message with '&':\\n"; 

    but you test for . 但是你测试. and not & 而不是&

     while(ch!='.'){ 
  • You pass an fstream to both openFile and readFile , but open and close the files in these functions. 您将fstream传递给openFilereadFile ,但是在这些函数中打开和关闭文件。 You can use a local variable instead. 您可以改用局部变量。

    Instead of 代替

     void openFile(fstream &fout){ ... fout.open(fileName.c_str(),ios::out); 

    you write 你写

     void openFile(){ ... fstream fout; fout.open(fileName.c_str(),ios::out); 

    or even shorter 甚至更短

     void openFile(){ ... ofstream fout(fileName.c_str()); 

    Additionally, you must change the declaration and call of openFile() . 此外,您必须更改openFile()的声明和调用。

    readFile and fin / ifstream work appropriately. readFilefin / ifstream正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM