我需要帮助创建filecomparison [英] I need help creating filecomparison

查看:58
本文介绍了我需要帮助创建filecomparison的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个名为FileComparison的程序,比较两个文件。首先,使用文本编辑器(如记事本)保存您喜欢的电影报价。接下来,复制文件内容,并将它们粘贴到一个单词中。然后编写文件 - 比较应用程序,显示两个文件的大小以及它们的大小相互之间的比例。要发现文件大小,可以使用如下语句创建system.IO.FileInfo对象,其中File_Name是包含文件名的字符串,并且size已声明为整数:FileInfo文件Info = new FileInfo(File_Name)Size =文件信息。长度;



我尝试过:



<$ p $使用系统;
使用System.IO;
公共类FileComparison
{
public FileComparison()
{
}

public static void Main()
{
const string WORD_FILE = @C:\ Users \Heather \Desktop\Week 9 / Quote.doc;
const string NOTEPAD_FILE = @C:\ Users \Heather\Desktop\Week 9 / Quote.txt;
long wordSize;
long notepadSize;
双倍比率;
FileInfo wordInfo = new FileInfo(WORD_FILE);
FileInfo notepadInfo = new FileInfo(NOTEPAD_FILE);
wordSize = wordInfo.Length;
notepadSize = notepadInfo.Length;
Console.WriteLine(word文件的大小是+ wordSize +/ n和记事本文件的大小是+ notepadSize);
ratio =(double)notepadSize / wordSize;
Console.WriteLine(记事本文件是单词文件大小的{0},ratio.ToString(P2));
}
}

解决方案

 wordSize =  @  C:\ Users \Heather Jacobs\Desktop\Week 9 / Quote.doc .Length; 



这将为您提供字符串中的字符数,而不是文件的大小。按照说明中的说明查看.NET中的 FileInfo 类:

引用:

要发现文件大小,可以创建system.IO.FileInfo


引用:

它没有连接到文件然后读取并告诉我文件的大小。我以为它会告诉我每个文件的大小和比例。我不知道我做错了什么不能工作?



首先检查文件的路径 - 它们不一样,其中一个很可能错误:

 const string WORD_FILE = @C:\ Users \Heather \Desktop \Week 9 / Quote.doc; 
const string NOTEPAD_FILE = @C:\ Users \Heather\Desktop\Week 9 / Quote.txt;

请记住,代码中的空格很重要!

如果这似乎无法解决问题,请使用调试器确切了解发生的情况:Put函数第一行的断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


抱歉,我们无法为您执行此操作 - 我们无法访问您的文件系统 - 您有时间学习一种新的(非常非常有用的)技能:调试!


creating a program named FileComparison that compares two files.First, use a text editor such as Notepad to save your favorite movie quote. next, copy the file contents , and paste them into a word. then write the file -comparison application that displays the sizes of the two files as well as the ratio of their sizes to each other. To discover a files size , you can create a system.IO.FileInfo object using statements such as the following, where File_Name is a string that contains the name of the file, and size has been declared as an integer: FileInfo file Info=new FileInfo(File_Name) Size=file Info. Length;

What I have tried:

using System;
using System.IO;
public class FileComparison
    {
    public FileComparison()
    {
    }

    public static void Main()
    {
        const string WORD_FILE = @"C:\Users\Heather \Desktop\Week 9/Quote.doc";
        const string NOTEPAD_FILE = @"C:\Users\Heather\Desktop\Week 9/Quote.txt";
        long wordSize;
        long notepadSize;
        double ratio;
        FileInfo wordInfo = new FileInfo(WORD_FILE);
        FileInfo notepadInfo = new FileInfo(NOTEPAD_FILE);
        wordSize =wordInfo.Length;
        notepadSize = notepadInfo.Length;
        Console.WriteLine("The size of the word file is" + wordSize + "/nand the size of the Notepad file is " + notepadSize);
        ratio = (double)notepadSize / wordSize;
        Console.WriteLine("The Notepad file is {0} of the size of the word file ", ratio.ToString("P2"));
        }
    }

解决方案

wordSize = @"C:\Users\Heather Jacobs\Desktop\Week 9/Quote.doc".Length;


That will give you the number of characters in the string, not the size of the file. Take a look at the FileInfo class in .NET, as stated in your instructions:

Quote:

To discover a files size , you can create a system.IO.FileInfo


Quote:

It is not connecting to the files and reading then and telling me what the size of the files are. I thought that it would tell me the size of each file and ratio for them. I do not see what I am doing wrong for it not to work?


Start by checking the paths to the files - they aren't the same, and one of them is very probably wrong:

const string WORD_FILE = @"C:\Users\Heather \Desktop\Week 9/Quote.doc";
const string NOTEPAD_FILE = @"C:\Users\Heather\Desktop\Week 9/Quote.txt";

Remember, spaces are significant in code!
If that doesn't appear to fix it, use the debugger to find out exactly what is going on: Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - we don't have any access to your file system - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于我需要帮助创建filecomparison的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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