字符串操作程序不返回预期输出 [英] String manipulation program not returning expected output

查看:49
本文介绍了字符串操作程序不返回预期输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



嗨。我写了一个小程序来学习用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


但是我得到了其他人混乱的人物。

解决方案

Logan Lee< Lo ******* **@student.uts.edu.auwrites:


嗨。我已经写了一个小程序来学习用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返回B * /

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] = =''\'''){

休息;

}

较小[i] =较大[a + i]; < br $>
}

返回较小;

}


/ * 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(large,a,b);

printf("%s",small );

}


/ *最后,text_file * /

abcdefghi


如果我用gcc * .c -o main编译它们并执行


main text_file 3 3


我在期待输出


def


但是我的其他人都是乱七八糟的人物。



我忘了包含头文件,但从给出的代码中可以看出它们很明显。


> int m = 0;


while(feof(text_file)== 0){

fgetc(text_file); m ++;

}

fclose(text_file);

fopen(" text_file"," r");



- >>>>>> char file_content [m];


你使用哪种编译器?

突出显示的行让我产生怀疑。你可以这样做吗?


12月15日下午1:40,krishan< srikrishanma ... @ gmail.comwrote:
< blockquote class =post_quotes>


int m = 0;

while(feof(text_file)== 0){

fgetc(text_file); m ++;

}

fclose(text_file);

fopen(" text_file"," r");



- >>>>>> 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.c
The 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 file
FILE *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.c
Return 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 */
abcdefghi

If I compile them with gcc *.c -o main and execute by

main text_file 3 3

I''m expecting the output

def

But I''m getting others with jumbled characters.

解决方案

Logan Lee <Lo*********@student.uts.edu.auwrites:

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.c
The 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 file
FILE *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.c
Return 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 */
abcdefghi

If I compile them with gcc *.c -o main and execute by

main text_file 3 3

I''m expecting the output

def

But I''m getting others with jumbled characters.

I forgot to include header files but they are obvious from the code given.


>int m=0;

while(feof(text_file)==0) {
fgetc(text_file); m++;
}
fclose(text_file);
fopen("text_file","r");

-->>>>>> char file_content[m];

Which compiler are u using?
The highlighted line makes me suspicious. can u do that ?


On Dec 15, 1:40 pm, krishan <srikrishanma...@gmail.comwrote:

int m=0;
while(feof(text_file)==0) {
fgetc(text_file); m++;
}
fclose(text_file);
fopen("text_file","r");


-->>>>>> 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.....


这篇关于字符串操作程序不返回预期输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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