C++ 如何获得C++;?
所以老师提出了这个任务: 你已经被联合网络司令部雇佣来执行法律,并且你得到了包含你必须解密的空密码的文件 因此,对于给定的第一个文件(例如),每隔一个字母都是正确的(即:“hielqlpo”是hello(假设您以第一个字母开头)。我的第一个问题是,如何读取文件?该文件位于我的桌面上的一个文件夹中,文件名为document01.cry。我不确定将该文件放入程序所需的命令 <>我也不太确定如何去拿一封信,跳过一封信,但老实说,我想在我发表这个问题之前先把它修整一下。所以现在……我的问题是在标题中所说的:你如何在C++中获取一个文件? <如果它有区别(我确信它),我使用Visual C++ 2008 Express版(因为它是免费的,我喜欢它!我还附加了我目前所拥有的,请记住它是非常基本的……并且我添加了<代码> GETCHARE();结尾,所以当它正常运行时,窗口保持打开,这样我就可以看到它了。(因为Visual Express在运行完成后会立即关闭窗口。) 迄今为止的守则:C++ 如何获得C++;?,c++,file,io,C++,File,Io,所以老师提出了这个任务: 你已经被联合网络司令部雇佣来执行法律,并且你得到了包含你必须解密的空密码的文件 因此,对于给定的第一个文件(例如),每隔一个字母都是正确的(即:“hielqlpo”是hello(假设您以第一个字母开头)。我的第一个问题是,如何读取文件?该文件位于我的桌面上的一个文件夹中,文件名为document01.cry。我不确定将该文件放入程序所需的命令 我也不太确定如何去拿一封信,跳过一封信,但老实说,我想在我发表这个问题之前先把它修整一下。所以现在……我的问题是在标题中所说的:
#include<iostream>
using namespace std;
int main()
{
while (! cin.eof())
{
int c = cin.get() ;
cout.put(c) ;
}
getchar();
}
#包括
使用名称空间std;
int main()
{
而(!cin.eof())
{
int c=cin.get();
cout.put(c);
}
getchar();
}
PS:我意识到这个代码抓住并显示了每个字符。现在,好了,一旦我能在文件中读到,我想我可以从那里修补它。我也在拨弄一两本我在C++上看到的东西,突然出现,尖叫着“选我!”再次感谢! 编辑::同样好奇的是,有没有办法输入您想要的文件? (即:
char文件名;
cout要执行文件操作,您需要正确的包括:
#include <fstream>
或者,对于输出:
ofstream outFile( "filename.txt", ios::out );
然后,您可以像使用cin一样使用infle,像使用cout一样使用outFile。完成后要关闭文件,请执行以下操作:
inFile.close();
outFile.close();
[编辑]包括对命令行参数的支持
[编辑]修复了可能的内存泄漏
[编辑]修复了缺少的引用
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char *argv){
ifstream infh; // our file stream
char *buffer;
for(int c = 1; c < argc; c++){
infh.open(argv[c]);
//Error out if the file is not open
if(!infh){
cerr << "Could not open file: "<< argv[c] << endl;
continue;
}
//Get the length of the file
infh.seekg(0, ios::end);
int length = infh.tellg();
//reset the file pointer to the beginning
is.seekg(0, ios::beg);
//Create our buffer
buffer = new char[length];
// Read the entire file into the buffer
infd.read(buffer, length);
//Cycle through the buffer, outputting every other char
for(int i=0; i < length; i+= 2){
cout << buffer[i];
}
infh.close();
}
//Clean up
delete[] buffer;
return 0;
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv){
ifstream infh;//我们的文件流
字符*缓冲区;
for(int c=1;c cerr我明白了!老实说,没有人回答有帮助,这是三者的结合,再加上他们的评论。非常感谢大家!我附上了我使用的代码,以及文档的副本。程序读取每个字符,然后吐出解密的文本。(即:1h.e0l/lqo是hello)下一步(额外学分)设置它,以便用户在读取下一个要输入的字符之前输入要跳过的字符数
这一步我打算自己做,但再次感谢大家的帮助!再次证明这个网站是多么棒,一次一行代码
编辑::代码经过调整以接受用户输入,并允许在不重新编译的情况下进行多次使用(我意识到它看起来像一团乱七八糟的东西,但这就是为什么它被评论得非常多…因为在我看来它看起来很漂亮、整洁)
#包括
#include//用于读取/写入文件,可以使用ifstream,但使用fstream是为了“以防万一”的感觉。
#包含文件名所需的/。
#include//for goto语句
使用名称空间std;
int main()
{
节目:
char choice;//允许用户选择是否执行其他文档。
char letter;//用于跟踪文档中的每个字符。
int x=1;//跟踪正确字母的第一个计数器。
int y=1;//第二个计数器(为了便于读取,使用1代替0,1是“第一个字符”)。
int z;//第三个计数器(用于检查前两个计数器是否相等)。
string filename;//允许用户输入他们希望使用的文件名。
cout虽然你的问题已经得到了回答,但有两个小提示:
1) 您可以执行以下操作,而不是通过计算x和y来查看是否在奇数或偶数字符上:
int count=0;
while(!infile.eof())
{
infile.get(letter);
count++;
if(count%2==0)
{
cout<<letter;
}
}
将使窗口在运行结束时保持打开状态,这样您就不需要最后一次getchar()调用(您可能需要#包含,这样才能工作)。非常感谢!Niyaz发布的网站也向我展示了这一点,但是,如果文件位于文件夹中(即:C:\Documents and Settings\J\Desktop\College work\CSIS 296\asst4\Documents),该怎么办?在我测试之前我会问,但我一点击“添加评论”就开始测试!如果你使用“filename.txt”,我发现它是有效的如果它与程序的.cpp文件位于同一文件夹中…但是如果它位于不同的文件中呢?如果我上面所说的内容有点混乱,您可以始终将文件名作为参数传递给程序foobar.exe c:\myFile.txt,然后检查该参数以确定要打开的文件恐怕我不理解您的意思,您能再解释一下吗?main(char*,int)函数有两个参数,一个字符串列表和传递的字符串数。在命令提示符下运行exe时,您可以这样传递这些参数:foobar.exe c:\a.txt(如果它位于不同的目录中,则具有完整路径)。从代码中打开此文件。看起来很棒!谢谢!:D但这仍然依赖于文件位于同一目录中,如果文件位于另一个目录中该怎么办?现在我们支持命令行参数并读取多个文件。例如foobar.exe myfile.txt myfile1.txt myfile2.txt我认为,虽然确实很棒,但这可能超出了范围到目前为止,我在课堂上学到了什么。我这么说是因为虽然它看起来确实很棒,而且我可以看出它的过程(有一点),但我不太理解它。我不太确定从哪里开始指出我的理解不足。我确实理解infh.seekg(查找文件的长度,从
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char *argv){
ifstream infh; // our file stream
char *buffer;
for(int c = 1; c < argc; c++){
infh.open(argv[c]);
//Error out if the file is not open
if(!infh){
cerr << "Could not open file: "<< argv[c] << endl;
continue;
}
//Get the length of the file
infh.seekg(0, ios::end);
int length = infh.tellg();
//reset the file pointer to the beginning
is.seekg(0, ios::beg);
//Create our buffer
buffer = new char[length];
// Read the entire file into the buffer
infd.read(buffer, length);
//Cycle through the buffer, outputting every other char
for(int i=0; i < length; i+= 2){
cout << buffer[i];
}
infh.close();
}
//Clean up
delete[] buffer;
return 0;
}
#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.
}
int count=0;
while(!infile.eof())
{
infile.get(letter);
count++;
if(count%2==0)
{
cout<<letter;
}
}
system("pause");