- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,对于我的 Systems 类中的作业,我们必须编写一个程序,该程序获取给定源中的文件并使用三个 copyfile() 函数之一将其复制到给定目标。
在命令行中运行程序时,它将接受三个参数:./cptest、输入文件位置和输出文件位置,以及两个可选参数:您要使用的 copyfile() 函数的编号。想要使用以及缓冲区大小。
如果为 copyfile() 函数参数选择 1,则程序应使用格式化 I/O(文件句柄)复制文件,然后一次复制一个字符。
如果为 copyfile() 函数参数选择 2,则程序应使用整数文件描述符复制文件,然后一次复制一个字符。
如果为 copyfile() 函数参数选择 3,则程序应分配一个大小等于您为缓冲区大小参数输入的大小(例如 1024)的缓冲区,然后使用 read() 从输入中读取一次最多归档这么多字节。
如果您不输入第三个参数(copyfile() 函数编号),则程序将自动使用 copyfile3() 以及您通过第四个参数决定的缓冲区大小。
如果您不输入第四个参数(缓冲区大小),则程序将默认将其设置为 1024,并在必要时使用它(copyfile1() 或 2() 不需要)。
在调用每个 copyfile() 函数之前和之后,程序都会使用 gettimeofday() 来制作时间戳。然后,程序使用后时间戳和前时间戳之间的秒和微秒差异创建一个新的时间戳,以查找复制花费的时间,然后打印所有这些信息。
例如,如果我输入: ./cptest ~/My_Documents/photo.JPG ~/assig6/photo.JPG 3程序应该返回:
复制前的时间戳: 秒:1425150842,微秒:914511复制后的时间戳: 秒:1425150842,微秒:927662复制耗时 0 秒,13151 微秒
既然您知道了该程序的作用,那么让我们开始讨论这个问题。每次我使用任何 copyfile() 函数运行程序时,无论是 1、2 还是 3,从技术上讲,复制工作正常,文件最终到达正确的目标,并且程序返回所有正确的信息时间戳方面。
但是,实际上,它不起作用,因为当我使用 copyfile1() 时,它会复制所有字节,但是当我尝试打开文件时 - 比如说 .jpg - 它说它无法查看文件,因为它似乎已损坏、损坏或太大。当我使用 copyfile2() 和 copyfile3() 时,目标中的文件只有 1 KB,当我不传入第三个或第四个参数时,输出文件有 0 KB。 Word 文档也会发生同样的情况。
是否有原因导致文件复制正常但文件在过程中损坏?看起来代码是对的,但最终结果却不对。
这是主文件 cptest.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include "cptest.h"
/** cptest.cpp
* A file copying program.
* Derived partially from caesar.cpp by Horstmann and Budd from big C++
*/
/** Prints usage instructions.
* @param program_name the name of this program
*/
void usage(char* program_name) {
// Prints out usage instructions if incorrect number of parameters is passed in
printf("Usage: %s infile outfile copy_function_number buffer_size \n", program_name);
}
/** Prints file opening error message
* @param filename the name of the file that could not be opened
*/
void open_file_error(char* filename) {
// Error message if unable to open the file
printf("Error opening file %s\n", filename);
}
/** Main program: copies a file.
* @param argc Number of command-line arguments (including program name).
* @param argv Array of pointers to character arays holding arguments.
* @return 0 if successful, 1 if failure.
*/
int main(int argc, char* argv[]) {
char* infilename; // Name of input file
char* outfilename; // Name of output file
int copy_function_number; // Chooses which copy function is used based on argv[3]
int buffer_size; // Size of buffer
int returnstatus; // 0 or 1 depending on success or failure to copy the file to the destination
struct timeval* before = malloc(sizeof(struct timeval)); // Struct for time before copying is done
struct timeval* after = malloc(sizeof(struct timeval)); // Struct for time after copying is done
if (argc != 3 && argc != 4 && argc != 5) {
usage(argv[0]); // Must have 2, 3, or 4 arguments in addition to ./cptest.
return 1; // Failure!
}
infilename = argv[1]; // Sets first parameter to the input file name
outfilename = argv[2]; // Sets second parameter to the output file name
if(argv[3] == NULL) {
copy_function_number = 3; // Program uses copyfile3 if no third parameter is entered
}
else {
copy_function_number = atoi(argv[3]); // Otherwise program uses whatever function is passed by third parameter
}
if (argv[4] == NULL) {
buffer_size = 1024; // Sets buffer size to 1024 if no fourth parameter is entered
}
else {
buffer_size = atoi(argv[4]); // Otherwise buffer size is whatever user enters as fourth parameter
}
if (copy_function_number == 1) {
gettimeofday(before, NULL); // Get timestamp before the copying
// Perform the copying with copyfile1() if the third parameter is 1
returnstatus = copyfile1(infilename, outfilename);
gettimeofday(after, NULL); // Get timestamp after the copying
}
if (copy_function_number == 2) {
gettimeofday(before, NULL); // Get timestamp before the copying
// Perform the copying with copyfile2() if the third parameter is 2
returnstatus = copyfile2(infilename, outfilename);
gettimeofday(after, NULL); // Get timestamp after the copying
}
if (copy_function_number == 3) {
gettimeofday(before, NULL); // Get timestamp before the copying
// Perform the copying with copyfile3() if the third parameter is 3
returnstatus = copyfile3(infilename, outfilename, buffer_size);
gettimeofday(after, NULL); // Get timestamp after the copying
}
else {
if (copy_function_number != 1 || copy_function_number != 2 || copy_function_number != 3 || argv[3] == NULL) {
gettimeofday(before, NULL); // Get timestamp before the copying
// Perform the copying with copyfile3() if no third parameter is entered
returnstatus = copyfile3(infilename, outfilename, buffer_size);
gettimeofday(after, NULL); // Get timestamp after the copying
}
}
struct timeval *copytime = difference_in_time(before, after); // Struct for time after copying is done
// Print out information of the timestamp before copying
printf("Timestamp Before Copying: \n Seconds: %d, Microseconds: %d\n", before->tv_sec, before->tv_usec);
// Print out information of the timestamp after copying
printf("Timestamp After Copying: \n Seconds: %d, Microseconds: %d\n", after->tv_sec, after->tv_usec);
// Print out information of the difference between the timestamps (how long the copying took)
printf("Copying took %d seconds, %d microseconds\n", copytime->tv_sec, copytime->tv_usec);
return returnstatus; // 0 if successful copy, 1 if unsuccessful.
}
/** Copies one file to another using formatted I/O, one character at a time.
* @param infilename Name of input file
* @param outfilename Name of output file
* @return 0 if successful, 1 if error.
*/
int copyfile1(char* infilename, char* outfilename) {
FILE* infile; //File handle for source.
FILE* outfile; // File handle for destination.
infile = fopen(infilename, "r"); // Open the input file.
if (infile == NULL) {
open_file_error(infilename); // Error message if there was a problem opening the input file.
return 1; // Failure!
}
outfile = fopen(outfilename, "w"); // Open the output file.
if (outfile == NULL) {
open_file_error(outfilename); // Error message if there was a problem opening the output file.
return 1; // Failure!
}
int intch; // Character read from input file. must be an int to catch EOF.
unsigned char ch; // Character stripped down to a byte.
// Read each character from the file, checking for EOF.
while ((intch = fgetc(infile)) != EOF) {
ch = (unsigned char) intch; // Convert to one-byte char.
fputc(ch, outfile); // Write out.
}
fclose(infile); // Close the input file.
fclose(outfile); // Close the output file.
return 0; // Success!
}
/** Copies one file to another using integer file descriptors instead of file handles, one character at a time.
* @param infilename Name of input file
* @param outfilename Name of output file
* @return 0 if successful, 1 if error.
*/
int copyfile2(char* infilename, char* outfilename) {
int infile; //File handle for source.
int outfile; // File handle for destination.
// Allocates a buffer for the chars that will be read from the input and written to the output.
char buffer[1024];
infile = open(infilename, O_RDONLY); // Open the input file.
if (infile < 0) {
open_file_error(infilename); // Error message if there was a problem opening the input file.
return 1; // Failure!
}
outfile = open(outfilename, O_WRONLY | O_CREAT); // Open the output file.
if (outfile < 0) {
open_file_error(outfilename); // Error message if there was a problem opening the output file.
return 1; // Failure!
}
int intchin; // Character read from input file. must be an int to catch EOF.
int intchout; // Character written to the output file. must be an int to catch EOF.
// Size of the buffer so that when you are copying extremely large files, it does not have to go through 200000 loop iterations
int buffer_size = 1024;
unsigned char ch; // Character stripped down to a byte.
// Read each character from the file, checking for 0.
while ((intchin = read(infile, buffer, buffer_size)) != 0) {
ch = (unsigned char) intchin; // Convert to one-byte char.
intchout = write(outfile, buffer, ch); // Write out.
}
close(intchin); // Close the input file.
close(intchout); // Close the output file.
return 0; // Success!
}
/** Copies one file to another using integer file descriptors, buffer_size characters at a time.
* @param infilename Name of input file
* @param outfilename Name of output file
* @param buffer_size Size of the buffer for reading and writing
* @return 0 if successful, 1 if error.
*/
int copyfile3(char* infilename, char* outfilename, int buffer_size) {
int infile; //File handle for source.
int outfile; // File handle for destination.
// Allocates a buffer of size buffer_size for the chars that will be read from the input and written to the output.
char* buffer = (char*) malloc(sizeof(char)* buffer_size);
infile = open(infilename, O_RDONLY); // Open the input file.
if (infile < 0) {
open_file_error(infilename); // Error message if there was a problem opening the input file.
return 1; // Failure!
}
outfile = open(outfilename, O_WRONLY | O_CREAT); // Open the output file.
if (outfile < 0) {
open_file_error(outfilename); // Error message if there was a problem opening the output file.
return 1; // Failure!
}
int intchin; // Character read from input file. must be an int to catch EOF.
int intchout; // Character written to the output file. must be an int to catch EOF.
unsigned char ch; // Character stripped down to a byte.
// Read each character from the file, checking for 0.
while ((intchin = read(infile, buffer, buffer_size)) != 0) {
ch = (unsigned char) intchin; // Convert to one-byte char.
intchout = write(outfile, buffer, ch); // Write out.
}
close(intchin); // Close the input file.
close(intchout); // Close the output file.
free(buffer); // Free the buffer that was allocated.
return 0; // Success!
}
/** Makes a new timeval struct that determines the difference between two timestamps
* @param time1 Struct containing the information for the first timestamp
* @param time2 Struct containing the information for the second timestamp
* @return The struct made using the two parameters
*/
struct timeval* difference_in_time(struct timeval* time1, struct timeval* time2) {
struct timeval* copytime = malloc(sizeof(struct timeval)); // Allocates a struct to hold the difference between the two timestamps.
if ((time2->tv_sec - time1->tv_sec) < 0) {
// Error message for if the first timestamp entered was before the second timestamp.
printf("Seconds value is negative! time1 should be before time2!\n");
}
if ((time2->tv_usec - time1->tv_usec) < 0) {
// Handles if the difference in microseconds between the second and first timestamps would be negative, subtracting 1 from the seconds.
copytime->tv_sec = (time2->tv_sec - time1->tv_sec) - 1;
// Handles if the difference in microseconds between the second and first timestamps would be negative, subtracting the difference from 1000000.
copytime->tv_usec = 1000000 - (time2->tv_usec - time1->tv_usec);
}
else {
// Otherwise the seconds for the third timestamp is the difference between the seconds of the second and first timestamps.
copytime->tv_sec = (time2->tv_sec - time1->tv_sec);
// Otherwise the microseconds for the third timestamp is the difference between the microseconds of the second and first timestamps.
copytime->tv_usec = (time2->tv_usec - time1->tv_usec);
}
return copytime; // Return the new timestamp created.
}
这是头文件 cptest.h:
#ifndef CPTEST_H
#define CPTEST_H
// function prototypes
void usage(char* program_name);
void open_file_error(char* filename);
int copyfile1(char* infilename, char* outfilename);
int copyfile2(char* infilename, char* outfilename);
int copyfile3(char* infilename, char* outfilename, int buffer_size);
struct timeval* difference_in_time(struct timeval* time1, struct timeval* time2);
#endif
这是 makefile:
cptest: cptest.o
gcc -g cptest.o -o cptest
cptest.o: cptest.c cptest.h
gcc -c -g cptest.c
clean:
rm -f *.o cptest
docs:
doxygen
chmod a+r html/*
cp -p html/* ~/public_html/cs2303assig6
编辑:认为使用我们需要包含在程序中的 readme.txt 作为示例可能更容易向您展示发生的情况。将其缩短为有用的信息,这样您就不必穿过更多的文字墙。
原文:
Name: *My name*
Section: CS2303 C01
What Program Does: *Explanation of what program does*
Example: *Example of what should show up when running program in the command line*
Results: *Test data for time it took for each function and using different buffer sizes to find most efficient function and buffer size*
Compiling:
To compile the code, use 'make' in the command line. The makefile should already link all of the .o files to the executable, so the work is done for you.
使用copyfile1()时的结果:
so the work is done for you.S2303 C01
What Program Does: *Explanation of what program does*
Example: *Example of what should show up when running program in the command line*
Results: *Test data for time it took for each function and using different buffer sizes to find most efficient function and buffer size*
Compiling:
To compile the code, use 'make' in the command line. The makefile should already link all of the .o files to the executable, so the work is done for you.
使用 copyfile2() 和 3() 时的结果:
so the work is done for you.
最佳答案
copyfile2 和 3 完全损坏,因为它们将读取的字符数转换为 char。由于缓冲区大小通常为 1024,因此此转换给出的结果为 0,因此不会写入任何内容。
对于副本文件 1,我建议使用二进制标志(“rb”或“wb”)打开文件,尽管我不确信这是问题所在。
关于c - 程序复制文件不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28786276/
这个问题已经有答案了: How to do case insensitive string comparison? (23 个回答) 已关闭 3 年前。 用户在我的输入栏中写入“足球”,然后执行第 6
啊,不习惯 javascript 中的字符串。 character_id= + id + correct= + correctOrIncorrect 这就是我需要制作成字符串的内容。如果您无法猜测字符
$(function() { var base_price = 0; CalculatePrice(); $(".math1").on('change', function(e) { Calc
我找不到任何文章回答问题:将Spinnaker部署到Spinnaker将管理的同一Kubernetes集群是否安全/正确?我主要是指生产,HA部署。 最佳答案 我认为Spinnaker和Kuberne
我正在使用MSVC在Windows上从源代码(官方源代码发布,而不是从仓库中)构建Qt5(Qt 5.15.0)。 我正在设置环境。变量,依赖项等,然后运行具有1600万个选项的configure,最后
我需要打印一个包含重复单词的数组。我的数组已经可以工作,但我不知道如何正确计算单词数。我已经知道,当我的索引计数器 (i) 为 49 时,并且当 (i) 想要计数到 50 时,我会收到错误,但我不知道
我正在遵循一个指南,该指南允许 Google map 屏幕根据屏幕尺寸禁用滚动。我唯一挣扎的部分是编写一个代码,当我手动调整屏幕大小时动态更改 True/False 值。 这是我按照说明操作的网站,但
我有一个类“FileButton”。它的目的是将文件链接到 JButton,FileButton 继承自 JButton。子类继承自此以使用链接到按钮的文件做有用的事情。 JingleCardButt
我的 friend 数组只返回一个数字而不是所有数字。 ($myfriends = 3) 应该是…… ($myfriends = 3 5 7 8 9 12). 如果我让它进入 while 循环……整个
这个问题在这里已经有了答案: Is there a workaround to make CSS classes with names that start with numbers valid?
我正在制作一个 JavaScript 函数,当调整窗口大小时,它会自动将 div 的大小调整为与窗口相同的宽度/高度。 该功能非常基本,但我注意到在调整窗口大小时出现明显的“绘制”滞后。在 JS fi
此问题的基本视觉效果可在 http://sevenx.de/demo/bootstrap-carousel/inc.carousel/tabbed-slider.html 获得。 - 如果你想看一看。
我明白,如果我想从函数返回一个字符串文字或一个数组,我应该将其声明为静态的,这样当被调用的函数被返回时,内容就不会“消亡”。 但我的问题是,当我在函数内部使用 malloc 分配内存时会怎样? 在下面
在 mySQL 数据库中存储 true/false/1/0 值最合适(读取数据消耗最少)的数据字段是什么? 我以前使用过一个字符长的 tinyint,但我不确定它是否是最佳解决方案? 谢谢! 最佳答案
我想一次读取并处理CSV文件第一行中的条目(例如打印)。我假设使用Unix风格的\n换行符,没有条目长度超过255个字符,并且(现在)在EOF之前有一个换行符。这意味着它是fgets()后跟strto
所以,我们都知道 -1 > 2u == true 的 C/C++ 有符号/无符号比较规则,并且我有一种情况,我想有效地实现“正确”比较。 我的问题是,考虑到人们熟悉的尽可能多的架构,哪种方法更有效。显
**摘要:**文章的标题看似自相矛盾。 本文分享自华为云社区《Java异常处理:如何写出“正确”但被编译器认为有语法错误的程序》,作者: Jerry Wang 。 文章的标题看似自相矛盾,然而我在“正
我有一个数据框,看起来像: dataDemo % mutate_each(funs(ifelse(. == '.', REF, as.character(.))), -POS) # POS REF
有人可以帮助我使用 VBScript 重新格式化/正确格式化带分隔符的文本文件吗? 我有一个文本文件 ^分界如下: AGREE^NAME^ADD1^ADD2^ADD3^ADD4^PCODE^BAL^A
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是一名优秀的程序员,十分优秀!