你如何在C ++中获得文件? [英] How do you get a file in C++?

查看:107
本文介绍了你如何在C ++中获得文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以老师已经提出了这个任务:

So the teacher has posed this assignment:

你被联合网络执法部门雇用了,而且你已经获得了包含空密码的文件解密。

You have been hired by the United Network Command for Law Enforcement, and you have been given files containing null cyphers you have to decrypt.

因此对于给出的第一个文件(作为示例),每隔一个字母都是正确的(即:'hielqlpo'是你好的(假设你从第一个开始)我的第一个问题是,如何读取文件?文档在我的桌面上的文件夹中,文件名为document01.cry。我不确定我需要将该文件放入程序的命令。

So for the first file given (as an example), every other letter is correct (ie: 'hielqlpo' is hello (assuming you start with the first letter). My first question is, how do I read in a file? The document is on my desktop in a folder and the file is named document01.cry. I'm not sure the command I need to put that file into the program.

我也不太确定如何抓住一封信并跳过一封信,但老实说我想在发布这个问题之前修补它!所以现在...我的问题如标题所述:你如何获取一个用C ++阅读的文件?

I'm also not overly sure how to grab a letter and skip a letter, but honestly I want to tinker with that before I post that question! So for now...my question is as stated in the title: How do you grab a file for reading in C++?

如果它有所作为(我确定它我正在使用Visual C ++ 2008 Express Edition(因为它是免费的,我喜欢它!我也附上了我的内容)到目前为止,请记住它是非常基本的......我在最后添加了 getchar(); ,所以当它运行正常时,窗口保持不变打开所以我可以看到它(因为Visual Express一旦完成运行就会关闭窗口。)

If it makes a difference (As I'm sure it does), I'm using Visual C++ 2008 Express Edition (because it's free and I like it! I've also attached what I have so far, please keep in mind it's -very- basic...and I added the getchar(); at the end so when it does run properly, the window stays open so I can see it (as Visual Express tends to close the window as soon as it's done running.)

到目前为止的代码:

#include<iostream>

using namespace std;

int main()
{
    while (! cin.eof())
    {
        int c = cin.get() ;
        cout.put(c) ;
    }
    getchar();
}

PS:我意识到这段代码会抓取并输出每一个字符。现在这很好,一旦我可以阅读文件,我想我可以从那里修补它。我也在用C ++上的一两本书来看看它弹出的任何东西并尖叫着接我!再次感谢!

PS: I realize that this code grabs and puts out every character. For now that's fine, once I can read in the file I think I can tinker with it from there. I'm also poking at a book or two I have on C++ to see it anything pops up and screams "Pick me!" Thanks again!

:还好奇,有没有办法输入你想要的文件?
(即:

: Also curious, is there a way to input the file you want? (I.e.:

char filename;
cout << "Please make sure the document is in the same file as the program, thank you!" << endl << "Please input document name: " ;
cin >> filename;
cout << endl;
ifstream infile(filename, ios::in);

此代码不它会回写一个错误,指出char无法转换为常量字符*。如何修复此问题?

This code doesn't work. It shoots back an error saying the char can't be converted to a const char *. How can this problem be fixed?

编辑2:
没关系说第2部分,我发现了!再次感谢您的帮助!

EDIT 2: Never mind about said part 2, I found it out! Thanks again for the assistance!

推荐答案

我想出来了!老实说,没有人帮忙,这是三者的结合,加上他们的评论。非常感谢你们!我附上了我使用的代码,以及文档的副本。程序读入每个字符,然后吐出破译的文本。(IE:1h.e0l / lqo是你好)下一步(额外的功劳)是设置它,所以用户在输入下一个字符之前输入要跳过的字符数。

I figured it out! To be honest no one answer helped, it was a combination of the three, plus the comments from them. Thank you all very much! I've attached the code I used, as well as a copy of the document. The program reads in every character, then spits out the deciphered text. (IE: 1h.e0l/lqo is hello) The next step (extra credit) is to set it up so the user inputs how many characters to skip before reading in the next character to input.

这一步我打算自己做,但是再次,非常感谢你们所有人大家的帮助!再次证明这个网站有多棒,一次一行代码!

This step I intend to do on my own, but again, thank you very much for all the assistance everyone! Proving once again how awesome this site is, one line of code at a time!

:代码调整为接受用户输入,以及允许多次使用重新编译(我意识到它看起来像一个巨大的草率混乱,但这就是为什么它非常评论...因为在我看来它看起来很漂亮和整洁)

: Code adjusted to accept user input, as well as allow for multiple uses without recompiling (I realize it looks like a huge sloppy mess, but that's why it's EXTREMELY commented...because in my mind it looks nice and neat)

#include<iostream>
#include<fstream>   //used for reading/writing to files, ifstream could have been used, but used fstream for that 'just in case' feeling.
#include<string>    //needed for the filename.
#include<stdio.h>   //for goto statement

using namespace std;

int main()
{
    program:
    char choice;  //lets user choose whether to do another document or not.
    char letter;  //used to track each character in the document.
    int x = 1;    //first counter for tracking correct letter.
    int y = 1;    //second counter (1 is used instead of 0 for ease of reading, 1 being the "first character").
    int z;        //third counter (used as a check to see if the first two counters are equal).
    string filename;    //allows for user to input the filename they wish to use.
    cout << "Please make sure the document is in the same file as the program, thank you!" << endl << "Please input document name: " ;
    cin >> filename; //getline(cin, filename);
    cout << endl;
    cout << "'Every nth character is good', what number is n?: ";
    cin >> z;   //user inputs the number at which the character is good. IE: every 5th character is good, they would input 5.
    cout << endl;
    z = z - 1;  //by subtracting 1, you now have the number of characters you will be skipping, the one after those is the letter you want.
    ifstream infile(filename.c_str()); //gets the filename provided, see below for incorrect input.
    if(infile.is_open()) //checks to see if the file is opened.
    {
        while(!infile.eof())    //continues looping until the end of the file.
        {   
                infile.get(letter);  //gets the letters in the order that that they are in the file.
                if (x == y)          //checks to see if the counters match...
                {
                    x++;             //...if they do, adds 1 to the x counter.
                }
                else
                {
                    if((x - y) == z)            //for every nth character that is good, x - y = nth - 1.
                    {
                        cout << letter;         //...if they don't, that means that character is one you want, so it prints that character.
                        y = x;                  //sets both counters equal to restart the process of counting.
                    }
                    else                        //only used when more than every other letter is garbage, continues adding 1 to the first
                    {                           //counter until the first and second counters are equal.
                        x++;
                    }
                }
        }
        cout << endl << "Decryption complete...if unreadable, please check to see if your input key was correct then try again." << endl;
        infile.close();
        cout << "Do you wish to try again? Please press y then enter if yes (case senstive).";
        cin >> choice;
        if(choice == 'y')
        {
            goto program;
        }
    }
    else  //this prints out and program is skipped in case an incorrect file name is used.
    {
        cout << "Unable to open file, please make sure the filename is correct and that you typed in the extension" << endl;
        cout << "IE:" << "     filename.txt" << endl;
        cout << "You input: " << filename << endl;
        cout << "Do you wish to try again? Please press y then enter if yes (case senstive)." ;
        cin >> choice;
        if(choice == 'y')
        {
            goto program;
        }
    }
    getchar();  //because I use visual C++ express.
}

::

我尝试插入文本,但我无法让它出来正确,它一直在处理一些字符,如编码(即撇号显然相当于粗体命令),但你可以尝试放置在0h1e.l9lao中没有括号到.txt,它应该给出相同的结果。

I tried inserting the text, but I couldn't get it to come out right, it kept treating some of the characters like coding (ie an apostrophe apparently is the equivalent of the bold command), but you could just try putting in "0h1e.l9lao" without the parenthesis into a .txt and it should give the same outcome.

再次感谢大家的帮助!

这篇关于你如何在C ++中获得文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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