了解C字符串串联 [英] Understanding C String Concatenation
问题描述
有关C中的assesment我得把两个文件在其中字符串,字符串来连接在一起例如:
For an assesment in C i have to take two files with strings within them, and a string to concatenate them together e.g:
如果文件1包含
Now is the
time for all
good men to
come to the aid of
the party.
和文件2包含:
alpha
beta
gamma
然后从输出
scat XX file1 file2
(SCAT是程序名)
(scat being the program name)
应
Now is theXXalpha
time for allXXbeta
good men toXXgamma
come to the aid ofXX
the party.XX
和
scat XX file2 file1
应
alphaXXNow is the
betaXXtime for all
gammaXXgood men to
XXcome to the aid of
XXthe party.
为了理解这一点,我一直在试图玩弄字符串连接手动尝试CONCAT串在一起。
In order to understand this i've been trying to play around with string concatenation trying to manually concat strings together.
我的问题是(A:怎么我访问文件的个别线路在C?)和B:如果我手动输入:
My question is (A: how to i access individual lines of a file in c?) and B: if I manually input:
char *str1 = "hello";
char *str2 = "world";
我怎么会一起添加这些字符串没有predefined功能。我最初的想法是使用一个for循环:
how would i add these strings together without a predefined function. My thoughts initially were to use a for loop:
for(str1; *str1 != '\0'; str1++)
if(*str1 == '\0')
*str1++ = *str2++;
我唯一的问题是,这是不是导致赛格故障由于内存访问?
my only issue is wouldn't this cause a seg fault due to memory access?
当我在一个string..how遇到'\\ 0'我可以扩展这个字符串?除非我只是两个字符串复制到一个新的char STR3 [200]
when i encounter a '\0' in a string..how can i extend this string? unless i just copy both strings into a new char str3[200] ?
(以上全部是在学习,这将是AP preciated尝试和帮助我了解字符串和字符串连接是如何工作的,任何帮助。)
(all of the above is to try and help me understand how strings and string concat works, any assistance in learning this would be appreciated.)
Euden
推荐答案
那么,有没有必要字符串连接,如果唯一的目标是从两个文件读取和写入到stdout,只有一个字符缓冲区是足够的,和一个简单的状态机就可以了。
Well, there is no need for string concatenation if the only goal is reading from two files, and writing to stdout, only a one-character buffer is sufficient, and a simple state machine will do.
#include <stdio.h>
int main (int argc, char **argv)
{
FILE *fp1, *fp2;
int ch, state ;
if (argc < 4) {
fprintf(stderr, "Argc=%d, should be > 3\n", argc);
return 0;
}
for (state = 0; state < 42; ) {
switch (state) {
case 0:
fp1 = fopen (argv[2], "r"); if (!fp1) return 0;
fp2 = fopen (argv[3], "r"); if (!fp2) { fclose (fp1); return 0;}
state++;
case 1:
ch = fgetc(fp1);
if (ch == EOF) state = 10;
else if (ch == '\n') state =2;
else putchar(ch);
break;
case 2:
fputs( argv[1], stdout);
state = 3;
case 3:
ch = fgetc(fp2);
if (ch == EOF) state = 22;
else if (ch == '\n') state =4;
else putchar(ch);
break;
case 4:
putchar(ch);
state = 1;
break;
case 10: /* fp1 exhausted */
ch = fgetc(fp2);
if (ch == EOF) state = 30;
else if (ch == '\n') state = 12;
else {
fputs( argv[1], stdout );
putchar(ch);
state = 11;
}
break;
case 11:
ch = fgetc(fp2);
if (ch == EOF) state = 13;
else if (ch == '\n') state = 12;
else putchar(ch);
break;
case 12:
putchar(ch);
state = 10;
break;
case 13:
putchar('\n');
state = 30;
break;
case 20: /* fp2 exhausted */
ch = fgetc(fp1);
if (ch == EOF) state = 30;
else if (ch == '\n') state = 21;
else putchar(ch);
break;
case 21:
fputs( argv[1], stdout);
state = 22;
case 22:
putchar('\n');
state = 20;
break;
case 30: /* both fp1+fp2 exhausted */
fclose (fp1);
fclose (fp2);
state = 42;
}
}
return 0;
}
免责声明:不要在家里尝试
Disclaimer: don't try this at home.
这篇关于了解C字符串串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!