一个程序,询问用户的文件名并打开它。 [英] A program that asks user for file name and opens it.

查看:51
本文介绍了一个程序,询问用户的文件名并打开它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一项已经评分的家庭作业 - 我只想知道自己为自己的利益做错了什么。这是我收到的评论:



fileAdder中有一个小错误,其中要读取的最后一个值被添加两次。看来这是一个问题Linux / Unix中的文本文件以换行符结尾。



我使用Mac终端。



This was a homework assignment that has already been graded - I just want to know what I did wrong for my own benefit. Here was the comment I received back:

"There's a minor bug in fileAdder where the last value to be read is being added twice. It appears that this is an issue with text files in Linux/Unix ending with a newline. "

I use a Mac terminal.

'
#include <iostream>
#include <fstream>  //Needed to use files.
#include <string>
using namespace std;

int main()
{
    //This declares the input and output files.
    ifstream input;
    ofstream output;
    string filename;
    
    int value, total = 0;
    
    cout << "Enter the file name: " << endl;
    cin >> filename;
    
    //This function opens file.
    input.open(filename.c_str());
    
    //This checks to see if the file can be opened, 
    //if yes, then it will calculate sum.
    if(input)
    {
        while(!input.eof()) 
        {
            input >> value;      
            total += value;
        }
    }
    
    //This closes the input file.
    input.close();
    
    //This opens the output file.
    output.open("sum.txt");
    
    //This is where a file will open and the calculated sum will be shown.
    if(output.is_open())
       output << total;

    //This is if the file can not be opened.
    else
       cout << "Could not access the file." << endl;
    
    //This closes the output file.
    output.close();

return 0;
}





以下是作业要求:



编写一个程序,提示用户输入文件名,然后尝试打开它。 open函数需要一个C风格的字符串(或字符串文字)而不是C ++字符串。幸运的是,有一个名为c_str()的函数将为您提供C ++字符串的C风格字符串版本。例如,您可以说inputFile.open(filename.c_str()),而不是说inputFile.open(filename)。如果输入文件在那里并且可以打开,程序应该读取文件中的整数列表,每行有一个整数,如下例所示:



14

9

12

$
$ b $ 30

8

109



注意:此示例仅用于演示输入文件的格式。你的程序不会将这些值输出到控制台或输出文件。



程序将把文件中的所有整数加在一起,打开一个输出文件调用sum.txt,并将总和写入该文件(只是那个数字 - 没有附加文本)。请记住关闭输入和输出文件。如果输入文件不存在(或存在但由于某种原因无法打开),程序应该打印出无法访问文件。



该文件必须命名为:fileAdder.cpp



我尝试过:



我更换:

int value,total = 0;



以下内容:



int value; //值是一些正数n

int total = 0; //总数保持前n个正数之和

int number; //数字量



Here is what the assignment asked for:

Write a program that prompts the user for the name of a file and then tries to open it. The open function needs a C-style string (or string literal) instead of a C++ string. Fortunately there is a function called c_str() that will give you the C-style string version of a C++ string. For example, instead of saying "inputFile.open(filename)", you can say "inputFile.open(filename.c_str())". If the input file is there and can be opened, the program should read the list of integers in the file, which will have one integer per line as in the following example:

14
9
12
-6
-30
8
109

Note: This example is just to demonstrate the format of the input file. Your program would not print these values out to the console or to the output file.

The program will then add together all the integers in the file, open an output file called sum.txt, and write the sum to that file (just that number - no additional text). Remember to close both the input and output files. If the input file is not there (or is there but couldn't be opened for some reason), the program should just print out "could not access file".

The file must be named: fileAdder.cpp

What I have tried:

I replace:
int value, total = 0;

with the following:

int value; // value is some positive number n
int total = 0; // total holds the sum of the first n positive numbers
int number; // the amount of numbers

推荐答案

复制问题 - 创建输入文件的副本,然后对其进行编辑,直到它与您的导师描述相符。然后使用您的应用程序来证明代码中存在的问题,方法是提供原始版本和未修改版本。

然后在调试器中运行您的应用程序并将其提供给显示问题的文件。按照代码执行,逐步执行每一行,并提前完成预期的操作。它做了你所期望的吗?如果是这样,继续。如果没有,为什么不呢?

这是一个叫做调试的过程,这是一项重要的技能。像所有技能一样,它必须用于开发 - 并且在这样的小应用程序上开发它比在100,000线怪物上开发要容易得多!

所以试一试,看看你能找到什么。



拼写错误[/ edit]
So duplicate the problem - create a copy of your input file, and edit it until it matches your tutors description. Then use your app to prove the problem exists in your code, by feeding it the original and unmodified versions.
Then run your app in the debugger and feed it the file that shows the problem. Follow the code through, stepping over each line, and working out in advance exactly what you expect to happen. Did it do what you expected? If so, continue. If not, why not?
This is a process called debugging, and it's an important skill. Like all skills though, it has to be used to develop - and it's a lot easier to develop it on a small app like this, than it is on a 100,000 line monster!
So give it a try, and see what you can find out.

[edit]typos[/edit]


这篇关于一个程序,询问用户的文件名并打开它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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