为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

慕九州9493288 的学生作业:

#include #include #include #include #define REQUIRED_PARAM_CNT 1 int get_current_time(const char *dest) { FILE *fp_out = NULL; time_t t = 0; struct tm *ptm = NULL; int write_len = 0; //避免空指针/空字符串 if (dest == NULL || *dest == '\0') { fprintf(stderr, "Err: Invalid file path (NULL/empty)\n"); return EXIT_FAILURE; } fp_out = fopen(dest, "w"); if (fp_out == NULL) { perror("Err: fopen failed"); return EXIT_FAILURE; } t = time(NULL); if (t == (time_t)-1) { perror("Err: time failed"); fclose(fp_out); return EXIT_FAILURE; } ptm = localtime(&t); if (ptm == NULL) { perror("Err: localtime failed"); fclose(fp_out); return EXIT_FAILURE; } printf("%d-%d-%d %d::%d::%d\n", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec); write_len = fprintf(fp_out, "%d-%d-%d %d::%d::%d\n", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec); if (write_len < 0) { perror("Err: fprintf failed"); fclose(fp_out); return EXIT_FAILURE; } fclose(fp_out); fp_out = NULL; return EXIT_SUCCESS; } int main(int argc, char const *argv[]) { if (argc != REQUIRED_PARAM_CNT + 1) { fprintf(stderr, "Usage: %s \n", argv[0]); return EXIT_FAILURE; } while (1) { if (get_current_time(argv[1]) == EXIT_FAILURE) { fprintf(stderr, "Update failed! Retry next second...\n"); } sleep(1); //睡眠1秒 } return EXIT_SUCCESS; } 【图片】

得分 100
学习任务

学无止境呀呀呀 的学生作业:

#include #include #include #include #include #include #define BUFFER_SIZE 4096 int copy_file(const char *src_path, const char *dest_path) { int src_fd = -1; int dest_fd = -1; ssize_t bytes_read, bytes_written; char buffer[BUFFER_SIZE]; int ret = -1; // 默认返回失败 // 1. 打开源文件(只读模式) src_fd = open(src_path, O_RDONLY); if (src_fd == -1) { fprintf(stderr, "无法打开源文件 '%s': %s\n", src_path, strerror(errno)); } // 2. 创建目标文件(只写模式,如果不存在则创建,权限为 0644) dest_fd = open(dest_path, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (dest_fd == -1) { fprintf(stderr, "无法创建目标文件 '%s': %s\n", dest_path, strerror(errno)); } // 3. 循环读取并写入文件内容 while ((bytes_read = read(src_fd, buffer, BUFFER_SIZE)) > 0) { char *out_ptr = buffer; ssize_t bytes_remaining = bytes_read; // 确保所有数据都被写入(处理部分写入的情况) while (bytes_remaining > 0) { bytes_written = write(dest_fd, out_ptr, bytes_remaining); if (bytes_written == -1) { fprintf(stderr, "写入文件 '%s' 失败: %s\n", dest_path, strerror(errno)); } bytes_remaining -= bytes_written; out_ptr += bytes_written; } } // 检查读取是否出错 if (bytes_read == -1) { fprintf(stderr, "读取文件 '%s' 失败: %s\n", src_path, strerror(errno)); } printf("文件复制成功: '%s' -> '%s'\n", src_path, dest_path); ret = 0; // 成功 return ret; } int main(int argc, char *argv[]) { // 参数检查 if (argc != 3) { fprintf(stderr, "用法: %s \n", argv[0]); return 1; } // 执行文件复制 return copy_file(argv[1], argv[2]); }

得分 100
讨论题

学渣小白 的学生作业:

#include #define debug 1 #define DEBUG_PRINT(…) do { if(debug) printf(VA_ARGS); } while(0) int main(void) { //DEBUG_PRINT (“Simplified debugging: temp=% d, formula calculation:% d \n”, temp, temp+10); int a[5] = {0}; int *p_max = &a[0]; printf(“Please enter 5 integers (separated by spaces or Enter):\n”); for (int i = 0; i < 5; i++) { if (scanf("%d", &a[i]) != 1) { printf(“Invalid input! Please enter integers.\n”); return 1; // 输入错误时退出 } } printf("\nArray contents:\n"); for (int i=0;i*p_max) p_max = &a[i]; } printf(”\n”); printf(“The maximum number is %d\n”,*p_max); return 0; } linux@linux:~/test01$ gcc 1l6practicer.c linux@linux:~/test01$ ./a.out Please enter 5 integers (separated by spaces or Enter): 88 78 55 23 80 Array contents: a[0]=88; a[1]=78; a[2]=55; a[3]=23; a[4]=80; The maximum number is 88 linux@linux:~/test01$ #include #define debug 0 #define DEBUG_PRINT(…) do { if(debug) printf(VA_ARGS); } while(0) int main(void) { //DEBUG_PRINT (“Simplified debugging: temp=% d, formula calculation:% d \n”, temp, temp+10); unsigned int data = 0x11223344; unsigned short *q = NULL; unsigned short t1 = 0; unsigned short t2 = 0; q = (unsigned short*)&data; DEBUG_PRINT ("*(unsigned int * )q=%#x\n",*(unsigned int * )q); t1=*(short* )q; DEBUG_PRINT ("t1=%#x\n",t1); t2=*((short* )q+1); DEBUG_PRINT ("t2=%#x\n",t2); printf("Output the sum of t1 and t2 is:\n%#x\n",t1+t2); printf("Output the difference of t1 and t2 is:\n%#X\n",t1-t2>0?t1-t2:t2-t1); return 0; } linux@linux:~/test01$ gcc 1l61practicer.c linux@linux:~/test01$ ./a.out Output the sum of t1 and t2 is: 0x4466 Output the difference of t1 and t2 is: 0X2222 linux@linux:~/test01$

得分 100
学习任务

慕九州9493288 的学生作业:

使用 fputs 与 fgets 来复制文件 #include #include #include #include #define REQUIRED_PARAM_CNT 2 /** * 拷贝文件 */ int copy_file(const char *src, const char *dest) { FILE *fp_in = NULL; FILE *fp_out = NULL; char *line_ptr = NULL; int ret = EXIT_SUCCESS; bool need_remove = false; char buf[1024] = {0}; //对源文件的传入做判空处理 if (src == NULL) { fprintf(stderr, "Error: Source file path is NULL!\n"); ret = EXIT_FAILURE; goto cleanup; } //对目标文件的传入做判空处理 if (dest == NULL) { fprintf(stderr, "Error: Target file path is NULL!\n"); ret = EXIT_FAILURE; goto cleanup; } //因后续用的fgets,这里“rb”仅支持纯文本,另外文本支持跨平台换行 fp_in = fopen(src, "rb"); if (fp_in == NULL) { perror("Error: Open source file failed (fopen) ==> "); ret = EXIT_FAILURE; goto cleanup; } //因后续用的fputs,这里“wb”仅支持纯文本,另外文本支持跨平台换行 fp_out = fopen(dest, "wb"); if (fp_out == NULL) { perror("Error: Open target file failed (fopen) ==> "); ret = EXIT_FAILURE; goto cleanup; } while (1) { line_ptr = fgets(buf, sizeof(buf), fp_in); if (line_ptr == NULL) { //读取结束(正常EOF)或者出错退出循环 break; } if (fputs(line_ptr, fp_out) == EOF) { //写入目标文件失败 perror("Error: Write target file failed (fputs) ==> "); need_remove = true; ret = EXIT_FAILURE; goto cleanup; } } if (ferror(fp_in)) //区分正常读完还是读取源文件失败,非0代表读取失败 { perror("Error: Read source file failed (ferror) ==> "); ret = EXIT_FAILURE; //源文件读取失败,会导致后续写入目标文件不完整或者为空,无效文件需要删除 need_remove = true; goto cleanup; } cleanup: if (fp_out != NULL) { fflush(fp_out); fclose(fp_out); fp_out = NULL; } if (fp_in != NULL) { fclose(fp_in); fp_in = NULL; } if (need_remove) { remove(dest); } if (ret == EXIT_SUCCESS) { printf("File copy successful!\n"); printf("Source file : %s\nTarget file : %s\n", src, dest); } return ret; } int main(int argc, char const *argv[]) { if (argc != REQUIRED_PARAM_CNT + 1) { fprintf(stderr, "Usage: %s \n", argv[0]); fprintf(stderr, "Note: Only support TEXT files (e.g., .txt, .c), not " "binary files (images/videos)\n"); return EXIT_FAILURE; } //规避用户输入相同的文件名 导致文件清空 size_t len1 = strlen(argv[1]); size_t len2 = strlen(argv[2]); size_t n = (len1 > len2) ? len1 : len2; if (strncmp(argv[1], argv[2], n) == 0) { fprintf(stderr, "Error: Source file and target file cannot be the same!\n"); return EXIT_FAILURE; } return copy_file(argv[1], argv[2]); } 【图片】

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号