Tag: printf

fprintffunction不起作用但它返回正数

我正在以下列方式使用fprintf 。 一切似乎都没问题,但fprintf根本不打印到我的文件中! fprintf(pFile, “%s\n”, “print”); 奇怪的是fprintf返回OK 。 它在上面的代码中返回6 ,但不打印到文件! 文件已成功创建但为空。 将它更改为printf正在打印,也可以。

C – 如何提示用户输入文件名

C编程很新。 我正在编写一个程序,我想提示用户输入要打开的文件名以供阅读。 在我显示的代码下面我想抛出错误,如果它没有打开或文件不存在,但是当我运行它时,我的代码爆炸,我必须关闭程序(DOS) /*ask user for the name of the file*/ printf(“enter file name: “); gets(fname); //Opens the file from where the text will be read. fp=fopen(fname, “r”); //Checks if the file us unable to be opened, then it shows the error message if (fp == NULL) { printf(“\nError, Unable to open the file for […]

如何在C中垂直打印?

我必须在一个循环中打印我想要以两行打印输出,而不是在一行上混合。 像这样: printf(“| %-7.2f “, Fahrenheit); 产生: | -508.00 | -463.00 | -418.00 | -373.00 | -328.00 | -283.00 | 当我添加printf(“| %-6d”, Celsius); 在上面的printf下面,它打印在我的第一个printf的中间/中间。 我希望输出如下: | -508.00 | -463.00 | -418.00 | -373.00 | -328.00 | -283.00 | | -300 | -275 | -250 | -225 | -200 | -175 | -150 | -125| 而不是将两组值混合在同一条线上。 […]

调用free()会导致程序崩溃

我有一个非常奇怪的问题,试图在分配的内存上调用free导致我的程序崩溃。 这是相关的代码: int i, count; char *specifier; char aisle[1]; count = 0; /*Find name of the new item and assign to the name field of new_node…*/ for (i = 0; input[i] != ‘,’; i++){ count++; } specifier = (char*)malloc((count+1)*sizeof(char)); if (specifier == NULL){ printf(“Out of memory. Shutting down.\n”); exit(EXIT_FAILURE); } for (i = 0; input[i] […]

C编程错误,打印链表,在运行时执行代码崩溃

我正在研究链表和指针。 这是一个包含推送function的简单代码。 在推送我的元素并尝试打印第一个成员后,执行的代码在运行时崩溃。 但是,当将相同的指针传递给print_list函数并在print_list中应用printf函数时,它可以正常工作。 但是当在main函数中直接使用它并应用printf函数时,它会崩溃。 #include #include typedef struct list{ int order; struct list *next; }list; void push(struct list **arg,int i); int main() { struct list **ptr=NULL; for(int i=0;iorder); //Here run time error return 0; } void push(struct list **arg,int i){ struct list *temp; temp= malloc(sizeof(list)); temp->order=i; temp->next=*arg; *arg=temp; } void print_list(list ** head) { […]

如果我的计算机是32位系统,它有32位地址吗? 但是当我在C中打印任何内存地址时,为什么我的地址<32bit?

例如 printf(“%u”,&a); 给我输出 65524 这是一个16位地址。

c在VS2013中编程,警告:printf():“当”不需要参数时参数太少?

嘿伙计们我把这个问题添加到类似的post中: 为什么会出现:警告:printf():第59行的参数太少 并被告知要打开一个新问题。 我的程序仍然可以编译并运行,但是在一个简单的printf语句中继续使用这个红色下划线,说的too few arguments 。 但它只是输出一条消息而不需要参数。 printf(“Demonstrating use of operators with an integer variable called ‘operation’…\n”); //<<-Warning printf("operation = %d and -operation = %d\n", operation, -operation); 我改变文本的内容并不重要,它总是带有红色下划线的警告。 它仍然编译并运行,但不知道它为什么一直警告我。 在添加普通printf输出简单消息之前,没有任何警告。 如果我将其删除并键入一个新的,它会回来。 如果我把它作为最后一行代码而不是第一行代码,它仍然会这样做。 知道可能导致此警告的原因是什么? 注意:在这部分代码之前我确实有很多其他的东西,这些是c文件中最后一个函数的最后两个语句。 让我知道你是否想要看到所有这些,但它很多。 所有这些只是基本的东西和卡车的评论(自己只是学习c的注释)。

select()函数最后不允许printf()没有“\ n”

我使用select()时遇到问题:它在我的程序中表现得很奇怪,我无法理解为什么。 #include #include int main() { char msg[1024]; fd_set readfds; int stdi=fileno(stdin); FD_SET(stdi, &readfds); for (;;) { printf(“Input: “); select(stdi+1, &readfds, NULL, NULL, NULL); if (FD_ISSET(stdi, &readfds)) { scanf(“%s”,msg); printf(“OK\n”); } } } 你期望什么程序行为? 可能和我一样(123是我输入的字符串): Input: 123 OK 但真正的程序行为如下所示: 123 Input: OK 让我们将调用printf(“Input:”)中的争论更改为“Input:\ n”。 我们得到的是 Input: 123 OK 所以select()函数是冻结输出,直到下一个以“\ n”结尾的printf()。 我能做些什么才能得到我期望的行为?

为两个几乎相同的代码显示不同的输出

在以下两个代码中我无法理解问题。 第一个代码是: #include main() { int num1, num2; scanf(“%d%d”, &num1, &num2); printf(“I LOVE MY INDIA\n”); //here is ‘\n’ after the statement printf(“%d”, num1/num2); return 0; } 这里,如果输入为num1=2且num2=0则在gcc编译器中输出为: 我爱我的印度 浮点exception(核心转储) 但对于第二个代码: #include main() { int num1, num2; scanf(“%d%d”, &num1, &num2); printf(“I LOVE MY INDIA”); //here is no ‘\n’ printf(“%d”, num1/num2); return 0; } 对于与之前相同的输入,显示: 浮点exception(核心转储) 在这两个代码之间只有一个区别。 […]

无法在字符串中添加int,尝试使用sprintf,但我遇到了麻烦

我正在尝试读取文件并打印文件中的所有单词,忽略所有其他空格和符号。 我有它使用strcpy,但它给了我一个错误,我正在尝试使用sprintf,但我真的不明白这个function的话。 它打印随机整数而不是字符串。 编辑:我对C完全不熟悉,所以我的指针不太好。 FILE *file; file = fopen(“sample_dict.txt”, “r”); int c; int wordcount = 0; int count = 0; const char *a[10]; char word[100]; do { c = fgetc(file); //error statement if (feof(file)) { break; } if (isalpha(c) && count == 2) { printf(“%s\n”, word); memset(word, 0, sizeof(word)); count = 1; wordcount++; } if […]