printf在ubuntu中不打印变量(C)

vktxenjb  于 2024-01-06  发布在  其他
关注(0)|答案(1)|浏览(394)

我有一个问题,打印字符串从一个变量使用'printf'和'%s'在c.当我编译下面的代码,并运行exe文件在windows中使用'<' oparand输入值从文本文件作为输入,字符串将打印罚款,你可以看到它在cmd,但是当我在ubunto中编译和运行elf文件并使用'<' parand时,变量中的字符串不会被打印出来,只有'%s'之后的字符会被打印到终端。
请注意,当使用puts只打印变量中的字符串时,它在ubuntu和windows中都可以工作,为什么会发生?

**当我直接从键盘输入时,'printf'在ubuntu中也能正常工作。
有人能解释一下为什么printf在从文件中获取输入时不起作用,以及是否有一种方法可以在ubuntu中格式化字符串?谢谢你的时间。
下面是一张cmd的图片,显示它在Windows上工作正常:

这里有一个Ubuntu终端的图片来显示问题:

正如你所看到的,变量和它之前的内容不会被打印出来。
(the问题在'initialze_names'函数中的第101行)在这段代码中:

  1. if (!is_name_unique(i, names[i]))
  2. {
  3. printf("The name '%s' is already exists in the array. Please Enter only uniqe names", names[i]);
  4. return 0;
  5. }

字符串
我正在使用这个Makefile为Ubuntu编译代码:

  1. CFLAGS = -Wall -ansi -pedantic
  2. all: get_name
  3. get_name: get_name.o
  4. gcc $(CFLAGS) get_name.o -o get_name
  5. rm get_name.o
  6. get_name.o: get_name.c
  7. gcc $(CFLAGS) -c get_name.c


这个命令用于Windows:

  1. gcc -Wall -ansi -pedantic -o get_name.exe get_name.c


对于那些有兴趣在完整的代码。

  1. /*
  2. * The program will let the user enter 30 uniuqe names and will call a function to get randomly a string from the array.
  3. * it will do it using the 'initialze_names' function to get the names and validate them, 'get_name' to get a random name from the array
  4. * and 'is_name_unique' to check if the name is uniuqe.
  5. *
  6. * this goal is achived by fisrt using initialze_names to get the user input and remove the new line character '\n'.
  7. * after that it will check if the string is uniuqe using the is_name_uniuqe function and.
  8. * then it wil print the array as it is and call get_name ten times and print the random name gotten each iteration.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. #define MAX_NAMES 30
  16. #define MAX_LENGTH 21
  17. #define NUMBER_OF_NAMES_TO_RANDOMIZE 10
  18. char names[MAX_NAMES][MAX_LENGTH] = { 0 };
  19. /* signature for the functions used in the code so the compiler will identify the functions regardless to where they are implemented */
  20. int initialze_names();
  21. char* get_name();
  22. int is_name_unique(int count, char newName[MAX_LENGTH]);
  23. void print_array();
  24. int comapre_strings(char str1[MAX_LENGTH], char str2[MAX_LENGTH]);
  25. int main()
  26. {
  27. int i = 0;
  28. char* random_name = NULL;
  29. /* Use current time as seed for random generator */
  30. srand((unsigned int)time(0));
  31. /* Calling initialze_names to get the user 30 uniqe names as input. */
  32. printf("Please feed the system with 30 uniqe names:\n");
  33. if (!initialze_names())
  34. {
  35. /* exiting the program if somthing went wrong in the inizialzation */
  36. return 0;
  37. }
  38. /* printing the names array to visualze the names incase the input got from a file. */
  39. print_array();
  40. printf("%d randomize names from the input:\n", MAX_NAMES);
  41. /* iterating 10 times and calling get_name each time to get random name from the array */
  42. for (i = 0; i < NUMBER_OF_NAMES_TO_RANDOMIZE; i++)
  43. {
  44. random_name = get_name();
  45. /* checking if the random name is returned */
  46. if (random_name != NULL)
  47. {
  48. /* printing the name if exists */
  49. puts(random_name);
  50. }
  51. }
  52. return 0;
  53. }
  54. /*
  55. * initialze_names function looping from 0 to MAX_NAMES and letting the user enter an input representing a name to each cell in the names array
  56. * then function will also be removing '\n' character if needed, clean the buffer in cases of overflow and for each name checking if it is unique.
  57. *
  58. * retrun: (int) - 0 if something went wrong in the proccess of getting the input and 1 if the the initialzation completed succesfuly.
  59. */
  60. int initialze_names()
  61. {
  62. int i = 0;
  63. char c = ' ';
  64. int length = 0;
  65. for (i = 0; i < MAX_NAMES; i++)
  66. {
  67. /* getting the user input and assaigning it to the array at index i */
  68. if (fgets(names[i], sizeof(names[i]), stdin) == NULL)
  69. {
  70. perror("Error reading input");
  71. return 0;
  72. }
  73. /* getting the name length */
  74. length = strlen(names[i]);
  75. if (length > 0 && names[i][length - 1] == '\n')
  76. {
  77. /* removing new line character '\n' if exists */
  78. names[i][length - 1] = '\0';
  79. }
  80. else
  81. {
  82. /* if string is to long, cleaning the buffer so the input won't overflow to next iteration. */
  83. while ((c = getchar()) != '\n' && c != EOF);
  84. }
  85. /* calling is_name_unique to check if the name already exists in the array */
  86. if (!is_name_unique(i, names[i]))
  87. {
  88. printf("The name '%s' is already exists in the array. Please Enter only uniqe names", names[i]);
  89. return 0;
  90. }
  91. }
  92. return 1;
  93. }
  94. /*
  95. * get_name function generating randomize index from 0 to 30 and uses it to get a random name from the list
  96. *
  97. * return: (char*) name[i] - a pointer to characters array containing the chosen name from the array
  98. */
  99. char* get_name()
  100. {
  101. /* randomizing the index */
  102. int i = rand() % MAX_NAMES;
  103. if (i > 0 && i < MAX_NAMES)
  104. {
  105. return names[i];
  106. }
  107. return NULL;
  108. }
  109. /*
  110. * is_name_unique getting the count and a new name check if it already exists in the array.
  111. *
  112. * input: (int) count - the index representing the last element position being inserted to the array.
  113. * input: (char*) newName - the new name that the user inserted.
  114. *
  115. * return: (int) - 0 if the name already exists in the array and 1 if it does not.
  116. */
  117. int is_name_unique(int count, char newName[MAX_LENGTH])
  118. {
  119. int i = 0;
  120. /* iterating from 0 up to the count - 1 to check if the same name comes anywhere before it in the array */
  121. for (i = 0; i < count; i++)
  122. {
  123. /* comparing the strings without case sensetivity. */
  124. if (comapre_strings(names[i], newName) == 0)
  125. {
  126. return 0;
  127. }
  128. }
  129. return 1;
  130. }
  131. /*
  132. * print_array iterating over the names array and printing each name and his index.
  133. */
  134. void print_array()
  135. {
  136. int i = 0;
  137. printf("\nThe names gotten from user:\n");
  138. for (i = 0; i < MAX_NAMES; i++)
  139. {
  140. printf("%d. %s ", i + 1, names[i]);
  141. }
  142. printf("\n\n");
  143. }
  144. /*
  145. * compare_strings is a function that gets two strings converting each string to lower case and returns 0 if they are equal to each other.
  146. *
  147. * input: (char*) str1 - the first string
  148. * input: (char*) str2 - the second string
  149. *
  150. * retrun: (int) - 0 if equal 1 if str1 grater then str2 and -1 if str1 smaller then str2
  151. */
  152. int comapre_strings(char str1[MAX_LENGTH], char str2[MAX_LENGTH])
  153. {
  154. int i = 0;
  155. /* iterating over str1 and converting him to lowercase */
  156. for (i = 0; i < strlen(str1); i++)
  157. {
  158. str1[i] = tolower(str1[i]);
  159. }
  160. /* iterating over str2 and converting him to lowercase */
  161. for (i = 0; i < strlen(str2); i++)
  162. {
  163. str2[i] = tolower(str2[i]);
  164. }
  165. /* comparing between the two and returning the result */
  166. return strcmp(str1, str2);
  167. }

zqdjd7g9

zqdjd7g91#

您用作输入的文本文件是在运行Windows时生成的,每行以CR+LF(\r\n)终止。
当在Ubuntu下读取时,CR被保留并显示为printf()。CR字符使以下文本显示在行的开头(CR =回车)。
解决方案:使用dos2unix实用程序将输入文件的CR+LF行结尾改为LF。

相关问题