如何通过终端将.in输入文件传入已编译的C程序?
解决C程序输入重定向问题及代码修正
一、正确的输入传入方法
你之前用<>操作符导致程序被覆盖,是因为这个操作符会同时重定向标准输入和输出到指定文件,且会截断(覆盖)已有文件。正确的输入传递方式有两种:
- 输入重定向:直接将输入文件内容作为程序的标准输入,执行命令:
把./your_compiled_program < input.inyour_compiled_program替换成你实际的程序文件名,input.in是你的输入文件。 - 管道传递:用
cat读取输入文件内容,通过管道传给程序:
这两种方法都不会修改你的程序文件,放心使用。cat input.in | ./your_compiled_program
二、你的C代码存在的致命问题(必须修复才能正常运行)
原代码有几处错误,会导致程序崩溃或输出结果错误:
getLongestString函数空指针访问:
初始时max设为NULL,调用strlen(max)会直接崩溃,因为NULL指针无法解引用。修正后:char* getLongestString(char** strings, int size) { if (size == 0) return NULL; char* max = strings[0]; for (int i = 1; i < size; i++) { if (strlen(max) < strlen(strings[i])) { max = strings[i]; } } return max; }数组越界访问:
printResults中words[size]超出了数组范围(数组索引从0到size-1),应该改为words[size-1]:void printResults(char** words, int size) { char* longest = getLongestString(words, size); printf("The longest word is: %s\n", longest); sortStrings(words, size); printf("The maximal word lexicographically is: %s\n", words[size-1]); printf("The minimal word lexicographically is: %s\n", words[0]); }内存泄漏:
freeWords只释放了每个字符串的内存,没有释放存储字符串指针的数组本身,需添加free(words):void freeWords(char** words, int size) { for (int i = 0; i < size; i++) { free(words[i]); } free(words); }输入残留换行符问题:
readSize中scanf("%d", &size)会留下换行符,可能导致后续readWord读取空内容,需添加getchar()吃掉换行:int readSize() { int size = 0; printf("Enter number of strings:\n"); scanf("%d", &size); getchar(); return size; }无效size后续执行问题:
main函数中如果size < 1,原代码未终止程序,会继续执行后续逻辑引发错误,需添加return 0:if (size < 1) { printf("Invalid size\n"); return 0; }
三、完整修正后的代码
#include <stdio.h> #include <string.h> #include <stdbool.h> #include <stdlib.h> #define BUFFER_SIZE 256 //============================================================================== /** swaps two strings */ void swap(char** a, char** b); /** Retrieves the maximal string by lexicographical order */ char* getLongestString(char** strings, int size); /** Sorts an array of strings */ void sortStrings(char** strings, int size); /** Reads an integer from the standard input */ int readSize(); /** Reads a string from the standard input and allocates memory for it */ char* readWord(); /** Reads strings from the standard input and allocates an array for them */ char** readWords(int size); /** Frees the memory of an allocated array of strings */ void freeWords(char** words, int size); /** Prints the required statistics of the strings array passed as parameter */ void printResults(char** words, int size); //============================================================================== void swap(char** a, char** b) { char* temp = *a; *a = *b; *b = temp; } char* getLongestString(char** strings, int size) { if (size == 0) return NULL; char* max = strings[0]; for (int i = 1; i < size; i++) { if (strlen(max) < strlen(strings[i])) { max = strings[i]; } } return max; } void sortStrings(char** strings, int size) { bool changed = true; while (changed) { changed = false; for (int i = 0; i < size - 1; i++) { if (strcmp(strings[i], strings[i + 1]) >= 0) { swap(&strings[i], &strings[i + 1]); changed = true; } } } } int readSize() { int size = 0; printf("Enter number of strings:\n"); scanf("%d", &size); getchar(); // 吃掉换行符 return size; } void printResults(char** words, int size) { char* longest = getLongestString(words, size); printf("The longest word is: %s\n", longest); sortStrings(words, size); printf("The maximal word lexicographically is: %s\n", words[size-1]); printf("The minimal word lexicographically is: %s\n", words[0]); } char* readWord() { char buffer[BUFFER_SIZE] = ""; scanf("%s", buffer); if (strlen(buffer) < 1) { return NULL; } char* str = malloc(strlen(buffer) + 1); if (!str) { return NULL; } return strcpy(str,buffer); } char** readWords(int size) { char** words = malloc(size * sizeof(*words)); if (words == NULL) { return NULL; } for (int i = 0; i < size; ++i) { words[i] = readWord(); if (words[i] == NULL) { freeWords(words, size); return NULL; } } return words; } void freeWords(char** words, int size) { for (int i = 0; i < size; i++) { free(words[i]); } free(words); // 释放数组本身 } int main() { int size = readSize(); if (size < 1) { printf("Invalid size\n"); return 0; // 终止程序避免后续错误 } char** words = readWords(size); if (words == NULL) { printf("Error reading words\n"); return 0; } printResults(words, size); freeWords(words, size); return 0; }
内容的提问来源于stack exchange,提问作者Netanel Barzilai
相关产品推荐
相关产品推荐

