我需要帮助来理解文件描述符
所以这是我的代码:
int main()
{
char ch;
close(1);
//now opening a file so that it gets the lowest possible fd i.e. 1
int fd=open("txt",O_RDWR);
//check..
printf("first printtf is executed\n");
scanf("%c",&ch);
printf("ur value is %c\n",ch);
printf("second printf is executed\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,我尝试将 的输出重定向printf到txt文件而不是标准输出,即终端。但是如何恢复标准输出文件描述符,以便printf在第二种情况下再次正常工作,即第二个printtf应该只向终端提供输出..
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 reading\n");
}
Run Code Online (Sandbox Code Playgroud)
// 您可以通过创建 name.txt 文件来测试这一点。如果您需要其他信息,请告诉我。
我只是想知道以下是否是将 int 转换为 char 的正确方法
#include <stdio.h>
int main()
{
int x = 500;
printf("%hhd\n", x);
}
Run Code Online (Sandbox Code Playgroud)
另外,从上面我想知道是否应该执行以下操作来显示字符的值。
#include <stdio.h>
int main()
{
char c = 'a';
printf("%hhd\n", c);
}
Run Code Online (Sandbox Code Playgroud)
还是会printf("%d\n", c);没事的?所以,基本上我试图通过 printf 输出整数的第一个字节而不进行任何转换。
#include<stdio.h>
int main()
{
printf("%s\n", "Hello");
printf("%s\n", &"Hello");
return 0;
}
Output :
Hello
Hello
Run Code Online (Sandbox Code Playgroud)
谁能向我解释为什么"Hello"并&"Hello"产生相同的结果?
我们中的许多人printf()在学会使用构造函数和析构函数之前很久就学会了使用。因此,当需要切换到 C++ 时,许多人会坚持printf()使用控制台输出。
有时你会听到:
printf()不好,你应该cout <<改用,因为它是 C++
放弃使用printf()而改用 有cout <<什么好处?
我在使用 select() 时遇到问题:它在我的程序中表现得很奇怪,我不明白为什么。
#include <stdio.h>
#include <netdb.h>
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
您期望什么程序行为?可能和我一样(123 是我输入的字符串):
Input: 123
OK
Run Code Online (Sandbox Code Playgroud)
但真正的程序行为是这样的:
123
Input: OK
Run Code Online (Sandbox Code Playgroud)
让我们将 call printf("Input: ") 中的争论改为 "Input: \n"。我们将得到的是
Input:
123
OK
Run Code Online (Sandbox Code Playgroud)
所以 select() 函数会冻结输出,直到下一个 printf() 以“\n”结尾。
我该怎么做才能获得预期的行为?
我一直在依赖,sprintf('%0.1f', 2.25) === '2.3'但事实证明它进来了2.2!
事实上,这似乎是随机的:
php > for ($j=0;$j<=10;$j++) { printf( "%s -> %0.1f\n",$j+ 0.05, $j+0.05); }
0.05 -> 0.1 // Up, as expected
1.05 -> 1.1 // Up, as expected
2.05 -> 2.0 // Down!
3.05 -> 3.0 // Down!
4.05 -> 4.0 // Down!
5.05 -> 5.0 // Down!
6.05 -> 6.0 // Down!
7.05 -> 7.0 // Down!
8.05 -> 8.1 // Up, as expected
9.05 -> 9.1 // Up, as expected
Run Code Online (Sandbox Code Playgroud)
我完全没有抓住重点吗?我觉得我脚下的地毯被拉开了,我在学校学到的一切都是错误的......!当然,一个对数字进行四舍五入的函数应该始终如一地做到这一点吗?(我注意到它 …
我知道整数算术将截断分数,但在这里至少应打印5。为什么要打印0.000000?
在下面的代码中:
#include <stdio.h>
int main(void) {
char* message = "Hello C Programmer!";
printf("%s", message);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不完全理解为什么没有必要'*'在printf通话中添加to消息。我是在假设条件下message,因为它是一个指针char,双引号字符串中的第一个字母,将显示该地址的'H'。
if(printf("Hello world")){}
Run Code Online (Sandbox Code Playgroud)
上面代码的输出是
Hello world
Run Code Online (Sandbox Code Playgroud)
我的朋友告诉我,函数printf返回的字符长度在此处非零,因此条件为true。
但是我不明白为什么它要执行该printf语句。不应该仅在内部将printf执行{ }吗?