本文介绍了字符串操作程序不返回预期输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨。我写了一个小程序来学习用C语写。但不幸的是输出都混乱了并且不好看。 / * read_file.c 整个此代码的一点是从文件中读取整个内容,然后将数据排列为单个字符串。 * / #include< stdio.h> char * returnArrayFromFile(char * file_name){ //尝试打开文件 FILE * text_file = fopen(file_name," r"); //文件中的字符总数。 int m = 0; while(feof(text_file)== 0){ fgetc(text_file); m ++; } fclose(text_file); fopen(" text_file"," r"); char file_content [m]; int i = 0; while(i< m){ fscanf(text_file,"%c",& file_content [i ++]); } fclose(text_file); 返回file_content; } / * substring.c 从ABC * / char * getSubstring(char * larger,int a,int b){ char * smaller =(char *)malloc(sizeof(char)* b); int i; for(i = 0; i< b; i ++){ if(large [a + i] ==''\\ \\ 0''){ 休息; } 较小[i] =较大[a + i]; } 返回较小; } / * main.c * / #include" read_file.h" #include" substring.h" int main(int argc,char * argv []){ char * file_name = argv [1]; int a = atoi(a rgv [2]); int b = atoi(argv [3]); char * larger = returnArrayFromFile(file_name); char * small = getSubstring(更大,a,b); printf("%s",更小); } / *最后,text_file * / abcdefghi 如果我用gcc * .c -o main编译它们并执行 main text_file 3 3 我期待输出 def 但是我得到了其他人混乱的人物。 解决方案 我忘了包含头文件,但从给出的代码中可以看出它们很明显。 - >>>>>> char file_content [m]; 你使用哪种编译器? 突出显示的行让我产生怀疑。你可以这样做吗? - >>>>>> char file_content [m]; 你使用哪种编译器? 突出显示的行让我产生怀疑。你能这样做吗? WOW gcc允许我这样做.....Hi. I''ve written a small program to learn to write in C. But unfortunately the output is all jumbled up and not nice./* read_file.cThe whole point of this code is to read the entire content from a file then arrange the data as a single string. */#include <stdio.h>char* returnArrayFromFile(char* file_name) {// Try opening a fileFILE *text_file=fopen(file_name,"r");// Total number of characters in the file.int m=0;while(feof(text_file)==0) {fgetc(text_file); m++;}fclose(text_file);fopen("text_file","r");char file_content[m];int i=0;while(i<m) {fscanf(text_file, "%c", &file_content[i++]);}fclose(text_file);return file_content;}/* substring.cReturn B from ABC */char* getSubstring(char* larger, int a, int b) {char* smaller=(char *)malloc(sizeof(char) * b);int i;for(i=0; i<b; i++) {if (larger[a+i]==''\0'') {break;}smaller[i]=larger[a+i];}return smaller;}/* main.c */#include "read_file.h"#include "substring.h"int main(int argc, char* argv[]) {char* file_name=argv[1];int a=atoi(argv[2]);int b=atoi(argv[3]);char* larger=returnArrayFromFile(file_name);char* smaller=getSubstring(larger, a, b);printf("%s", smaller);}/* Lastly, text_file */abcdefghiIf I compile them with gcc *.c -o main and execute bymain text_file 3 3I''m expecting the outputdefBut I''m getting others with jumbled characters. 解决方案I forgot to include header files but they are obvious from the code given.-->>>>>> char file_content[m];Which compiler are u using?The highlighted line makes me suspicious. can u do that ?-->>>>>> char file_content[m];Which compiler are u using?The highlighted line makes me suspicious. can u do that ?WOW gcc allows me to do that..... 这篇关于字符串操作程序不返回预期输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 11:32
查看更多