简体   繁体   English

fscanf更改字符串变量?

[英]fscanf changes string variable?

I'm a beginner in programming and I'm currently learning C, but I've come across something that confuses me. 我是编程的初学者,目前正在学习C,但是遇到了使我感到困惑的事情。

In my while loop i have: 在我的while循环中,我有:

char* bword = word
fscanf(fp, "%s", word);

Where word is a char[46], bword is a char* and fp is the file (a word dictionary in this case). 其中word是char [46],bword是char *,fp是文件(在这种情况下为单词字典)。 I wanted to keep track of the word before it gets replaced when scanning fp. 我想在扫描fp之前先跟踪该单词,然后再进行替换。 And it seemed logical to me to asign the word to a variable before it gets changed. 在我看来,在更改之前将单词分配给变量似乎是合乎逻辑的。 But if I print bword after fscanf(fp, '%s", word) then it's changed to the new word! I don't really understand why and how. 但是,如果我在fscanf(fp,'%s“,word)之后打印bword,那么它将被更改为新单词!我真的不明白为什么以及如何。

If I do this: 如果我这样做:

bword = word;
printf("1.%s\n", word);
printf("2.%s\n", bword);
// scan the file for a for a word and store in word
fscanf(fp, "%s", word);
printf("3.%s\n", word);
printf("4.%s\n", bword);

I get these results in the command line: 我在命令行中得到以下结果:

1. 
2. 
3.a
4.a

(You don't see anything before 1 and 2 because the word was empty at the time). (您不会在1和2之前看到任何内容,因为当时单词为空)。 I also tried assigning word to something "bar" before the loop but then I got: 我还尝试在循环之前将单词分配给“ bar”,但是随后得到:

1.bar
2.bar
3.a
4.a

So how do I solve this? 那么我该如何解决呢? I don't want bword to change. 我不想改变剑。

You need to understand that this 您需要了解

char *bword = word;

declares a pointer to the first element of the array word , which means that modifiying bword modifies word too, because bword is just a reference 1 to the real data location which is the array word 声明一个指向数组word的第一个元素的指针,这意味着修改bword bword修改word ,因为bword只是对实际数据位置即数组word的引用1

If you want bword and word to be independent of each other then make bword an array so it's stored in a different location, like this 如果您希望bwordword彼此独立,则将bword为数组,以便将其存储在不同的位置,如下所示

char bword[SOME_REASONABLE_SIZE];

from here you can call fscanf() and pass the appropriate array and the result will be the one you expected before. 从这里可以调用fscanf()并传递适当的数组,结果将是您之前期望的结果。

Notice, that when you pass the array to fscanf() , essentially you are passing a pointer to the first element of it, just what your bword was in the first place. 请注意,当您将数组传递给fscanf() ,实际上是将指针传递给了它的第一个元素,恰好是您的bword的第一位。


1 A reference in a general sense, not in c++ sense. 1 一般意义上的引用,而不是c ++意义上的引用。

First you must learn about pointers (you can see tutorialspoint ). 首先,您必须了解指针(您可以参阅tutorialspoint )。
Then you find out array is a pointer to a memory so when you assigning them you just assign addresses so if one of them change other one is also changed. 然后您发现数组是指向内存的指针,因此在分配它们时,您只分配了地址,因此,如果其中一个更改,另一个也会更改。 if you want to copy strings you can not assign then you must copy them and you can use strcpy() function. 如果要复制不能分配的字符串,则必须复制它们,然后可以使用strcpy()函数。 as it man page says it have following syntax: 如手册页所述,它具有以下语法:

char *strcpy(char *dest, const char *src);

The strcpy() function copies the string pointed to by src, including the terminating null byte ('\\0'), to the buffer pointed to by dest. strcpy()函数将src指向的字符串(包括终止的空字节('\\ 0'))复制到dest指向的缓冲区。 The strings may not overlap, and the destination string dest must be large enough to receive the copy. 字符串不能重叠,并且目标字符串dest必须足够大才能接收副本。 Beware of buffer overruns! 当心缓冲区溢出!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM