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

vktxenjb  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(243)

我有一个问题,打印字符串从一个变量使用'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行)在这段代码中:

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

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

CFLAGS = -Wall -ansi -pedantic

all: get_name

get_name: get_name.o
    gcc $(CFLAGS) get_name.o -o get_name
    rm get_name.o

get_name.o: get_name.c
    gcc $(CFLAGS) -c get_name.c


这个命令用于Windows:

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


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

/*
* The program will let the user enter 30 uniuqe names and will call a function to get randomly a string from the array.
* 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
* and 'is_name_unique' to check if the name is uniuqe.
*
* this goal is achived by fisrt using initialze_names to get the user input and remove the new line character '\n'.
* after that it will check if the string is uniuqe using the is_name_uniuqe function and.
* then it wil print the array as it is and call get_name ten times and print the random name gotten each iteration.
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>

#define MAX_NAMES 30
#define MAX_LENGTH 21
#define NUMBER_OF_NAMES_TO_RANDOMIZE 10

char names[MAX_NAMES][MAX_LENGTH] = { 0 };

/* signature for the functions used in the code so the compiler will identify the functions regardless to where they are implemented */
int initialze_names();
char* get_name();
int is_name_unique(int count, char newName[MAX_LENGTH]);
void print_array();
int comapre_strings(char str1[MAX_LENGTH], char str2[MAX_LENGTH]);

int main()
{
    int i = 0;
    char* random_name = NULL;

    /* Use current time as seed for random generator */
    srand((unsigned int)time(0));

    /* Calling initialze_names to get the user 30 uniqe names as input. */
    printf("Please feed the system with 30 uniqe names:\n");
    if (!initialze_names())
    {
        /* exiting the program if somthing went wrong in the inizialzation */
        return 0;
    }

    /* printing the names array to visualze the names incase the input got from a file. */
    print_array();

    printf("%d randomize names from the input:\n", MAX_NAMES);
    /* iterating 10 times and calling get_name each time to get random name from the array */
    for (i = 0; i < NUMBER_OF_NAMES_TO_RANDOMIZE; i++)
    {
        random_name = get_name();
        /* checking if the random name is returned */
        if (random_name != NULL)
        {
            /* printing the name if exists */
            puts(random_name);
        }
    }

    return 0;
}

/*
* 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
* 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.
*
* retrun: (int) - 0 if something went wrong in the proccess of getting the input and 1 if the the initialzation completed succesfuly.
*/
int initialze_names()
{
    int i = 0;
    char c = ' ';
    int length = 0;

    for (i = 0; i < MAX_NAMES; i++)
    {
        /* getting the user input and assaigning it to the array at index i */
        if (fgets(names[i], sizeof(names[i]), stdin) == NULL)
        {
            perror("Error reading input");
            return 0;
        }

        /* getting the name length */
        length = strlen(names[i]);
        if (length > 0 && names[i][length - 1] == '\n')
        {
            /* removing new line character '\n' if exists */
            names[i][length - 1] = '\0';
        }
        else
        {
            /* if string is to long, cleaning the buffer so the input won't overflow to next iteration. */
            while ((c = getchar()) != '\n' && c != EOF);
        }
        
        /* calling is_name_unique to check if the name already exists in the array */
        
        if (!is_name_unique(i, names[i]))
        {
            printf("The name '%s' is already exists in the array. Please Enter only uniqe names", names[i]);
            return 0;
        }
        
    }
    return 1;
}

/*
* get_name function generating randomize index from 0 to 30 and uses it to get a random name from the list
*
* return: (char*) name[i] - a pointer to characters array containing the chosen name from the array
*/
char* get_name()
{
    /* randomizing the index */
    int i = rand() % MAX_NAMES;
    if (i > 0 && i < MAX_NAMES)
    {
        return names[i];
    }
    return NULL;
}

/*
* is_name_unique getting the count and a new name check if it already exists in the array.
*
* input: (int) count - the index representing the last element position being inserted to the array.
* input: (char*) newName - the new name that the user inserted.
*
* return: (int) - 0 if the name already exists in the array and 1 if it does not.
*/
int is_name_unique(int count, char newName[MAX_LENGTH])
{
    int i = 0;

    /* iterating from 0 up to the count - 1 to check if the same name comes anywhere before it in the array */
    for (i = 0; i < count; i++)
    {
        /* comparing the strings without case sensetivity. */
        if (comapre_strings(names[i], newName) == 0)
        {   
            return 0;
        }
    }
    return 1;
}

/*
* print_array iterating over the names array and printing each name and his index.
*/
void print_array()
{
    int i = 0;

    printf("\nThe names gotten from user:\n");
    for (i = 0; i < MAX_NAMES; i++)
    {
        printf("%d. %s ", i + 1, names[i]);
    }
    printf("\n\n");
}

/*
* 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.
*
* input: (char*) str1 - the first string
* input: (char*) str2 - the second string
*
* retrun: (int) - 0 if equal 1 if str1 grater then str2 and -1 if str1 smaller then str2
*/
int comapre_strings(char str1[MAX_LENGTH], char str2[MAX_LENGTH])
{
    int i = 0;

    /* iterating over str1 and converting him to lowercase */
    for (i = 0; i < strlen(str1); i++)
    {
        str1[i] = tolower(str1[i]);
    }

    /* iterating over str2 and converting him to lowercase */
    for (i = 0; i < strlen(str2); i++)
    {
        str2[i] = tolower(str2[i]);
    }
    
    /* comparing between the two and returning the result */
    return strcmp(str1, str2);
}

zqdjd7g9

zqdjd7g91#

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

相关问题