- 论坛徽章:
- 0
|
今天使用printf打印的时候,发现一个很奇怪的问题:
代码:
printf("%lu\t\" %s\" != \"%s\"\n", lineNum, buff1, buff2);
printf("%s\n", buff1);
printf("%s \n", buff2);
实际打印值:
"!= "idasdfadafnt main(int argc, char **argv)
int main(int argc, char **argv)
dasdfadafnt main(int argc, char **argv)
即在第一行打印中并没有将buff1的内容打印出来,而后边却可以打印出来,分析了一会,第一句printf也没有发现什么问题,是何道理?
---源代码----- /* Exercise 7-6 - write a program to compare two files, printing the first line
- * where they differ
- *
- * Note : I amended this a bit...if a file is shorter than the other, but is identical
- * up to that point, the program prints out "EOF" as the string that's not equal.
- *
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define BUFF_SIZE 1000
- /* uses fgets, removes the '\n' at the end of the string if it exists */
- char *safegets(char *buffer, int length, FILE *file)
- {
- char *ptr;
- int len;
-
- if (buffer != NULL)
- {
- ptr = fgets(buffer, length, file);
-
- if (ptr != NULL)
- {
- len = strlen(buffer);
-
- if (len > 0)
- {
- if (buffer[len - 1] == '\n')
- {
- buffer[len - 1] = '\0';
- }
- }
- }
-
- return ptr;
- }
-
- return NULL;
- }
- int main(int argc, char *argv[])
- {
- FILE *leftFile, *rightFile;
- char buff1[BUFF_SIZE], buff2[BUFF_SIZE];
- char *ptr1, *ptr2;
- unsigned long lineNum = 0;
-
- if (argc < 3)
- {
- fprintf(stderr, "Usage : 7_6 <path to file> <path to file>\n");
- return 0;
- }
-
- if (!(leftFile = fopen(argv[1], "r")))
- {
- fprintf(stderr, "Couldn't open %s for reading\n", argv[1]);
- return 0;
- }
-
- if (!(rightFile = fopen(argv[2], "r")))
- {
- fprintf(stderr, "Couldn't open %s for reading\n", argv[2]);
- fclose(leftFile); /* RJH 10 Jul 2000 */
- return 0;
- }
-
- /* read through each file, line by line */
- ptr1 = safegets(buff1, BUFF_SIZE, leftFile);
- ptr2 = safegets(buff2, BUFF_SIZE, rightFile);
- ++lineNum;
-
- /* stop when either we've exhausted either file's data */
- while (ptr1 != NULL && ptr2 != NULL)
- {
- /* compare the two lines */
- if (strcmp(buff1, buff2) != 0)
- {
- printf("Difference:\n");
- printf("%lu\t\" %s\" != \"%s\"\n", lineNum, buff1, buff2);
- printf("%s\n", buff1);
- printf("%s \n", buff2);
- goto CleanUp;
- }
-
- ptr1 = safegets(buff1, BUFF_SIZE, leftFile);
- ptr2 = safegets(buff2, BUFF_SIZE, rightFile);
- ++lineNum;
- }
- /*
- * if one of the files ended prematurely, it definitely
- * isn't equivalent to the other
- */
- if (ptr1 != NULL && ptr2 == NULL)
- {
- printf("Difference:\n");
- printf("%lu\t\"%s\" != \"EOF\"\n", lineNum, buff1);
- }
- else if (ptr1 == NULL && ptr2 != NULL)
- {
- printf("Difference:\n");
- printf("%lu\t\"EOF\" != \"%s\"\n", lineNum, buff2);
- }
- else
- {
- printf("No differences\n");
- }
- CleanUp:
- fclose(leftFile);
- fclose(rightFile);
- return EXIT_SUCCESS;
- }
复制代码 |
|