标签: stdio

关于EOF和EOL的问题

我试图了解 EOF 和 EOL,以及 C++ iostream 的实际工作原理。

在将输入通过getchar()或输入getche()char变量中时,我发现如果我写这样的行:

char a;
a =  getche();  // it returns char '\r' if pressed enter
a =  getchar(); // it returns char '\n' if pressed enter
Run Code Online (Sandbox Code Playgroud)

为什么是这些值?

  1. 实际上是什么让 C++ 认为我们已经用完了输入(即是否总是'\n'让 C++ 认为它在其输入的末尾?)。

  2. 在读/写一个以字符串结尾的文件时,'\n' 如果行以NULL字符结尾会发生什么,这也代表行尾?

你能用例子简要解释所有这些吗?

c++ windows stdio

1
推荐指数
1
解决办法
5457
查看次数

C 中 cin.ignore() 的等价物是什么?

我知道 C++ 流函数是建立在 C 的stdio库之上的。

我必须在 C 中做什么才能获得与 相同的结果cin.ignore(n)
例如,我应该使用stdio函数fseek(stdin, n, 0)还是使用其他方法cin.ignore

c c++ stdin stdio cin

1
推荐指数
1
解决办法
3832
查看次数

C 中 tmpfile() 的用法

我不明白为什么我的下面的脚本似乎可以在不创建任何文件的情况下工作。

脚本.c:

#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]){
    printf("P_tmpdir is '%s'\n", P_tmpdir);
    FILE *tmp = tmpfile();
    if(tmp == NULL){
        printf("Unable to create temp file");
        exit(1);
    }
    else{
        printf("Temporary file is created\n");
    }
    for(int i = 0; string[i] != '\0'){
        fputc(string[i], tmp);
    }
    rewind(tmp);
    while(!feof(tmp)){
        putchar(fgetc(tmp));
    }
    sleep(3);
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

P_tmpdir 变量返回给我“/tmp”目录,尽管在睡眠期间没有在其中创建新文件...您能帮助我或解释一下吗?

c stdio temporary-files

1
推荐指数
1
解决办法
743
查看次数

如何使用stdio.h覆盖?

我想覆盖现有文件,这应该很简单:

#include <stdio.h>

int main()
{
    FILE *f;
    unsigned char ba;
    int i;

    f = fopen("junk", "wb");
    for (i = 1; i <= 10; i++)
        fputc(i, f);
    fclose(f);
    f = fopen("junk", "ab");
    fseek(f, 0, SEEK_SET);

    for (i = 1; i <= 5; i++) {
        printf("Position before: %ld\n", ftell(f));
        fputc(99, f);
        printf("Position after: %ld\n", ftell(f));
    }

    fclose(f);
    f = fopen("junk", "rb");

    for (i = 1; i <= 10; i++) { 
        ba = fgetc(f);
        printf("%d ", ba); 
    }

    printf("\n");
}
Run Code Online (Sandbox Code Playgroud)

结果是:

Position …
Run Code Online (Sandbox Code Playgroud)

c stdio

1
推荐指数
1
解决办法
176
查看次数

在库之间共享FILE指针

我有一个简单的问题; 我使用的是FILE* fp在我的图书馆之一,fopen/fwrite到该文件,我想这样做fclose()fp另一个库中-什么是其他库来获取文件句柄的最佳方式?

c io fopen file stdio

0
推荐指数
1
解决办法
200
查看次数

编译器错误消息与cout

我以前输错了错误信息.现在修好了.

我目前收到以下编译器错误消息

error: no match for 'operator<<' in 'std::cout << Collection::operator[](int)(j)'
Run Code Online (Sandbox Code Playgroud)

编译器抱怨的代码是

cout << testingSet[j];
Run Code Online (Sandbox Code Playgroud)

哪个testingSet类型的对象Collectionoperator[]重载以返回类型的对象Example. Example有一个operator<<为ostream和Example 重载的友元函数.

注意:这实际上在Visual Studio中编译得很好; 但是不使用g ++编译.

这是执行operator<<:

ostream& operator<<(ostream &strm, Example &ex)
{
     strm << endl << endl;
     strm << "{ ";
     map<string, string>::iterator attrib;
     for(attrib = ex.attributes.begin(); attrib != ex.attributes.end(); ++attrib)
     {
          strm << "(" << attrib->first << " = " << attrib->second << "), ";
     }
     return strm << …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors compilation stdio

0
推荐指数
1
解决办法
367
查看次数

C++(VS2012)stdio:如果该字节为09h,则无法读取单字节文件

代码很简单.

unsigned char a_byte;
ifstream a_file("C:/file.bin", ios_base::binary);
if (a_file.is_open() && a_file.good())
{
    a_file.seekg(0);
    a_file >> a_byte;
    a_file.close();
}
Run Code Online (Sandbox Code Playgroud)

问题是它不会从一个单字节文件读取09h - 我只是在a_byte var中得到零.它确实适用于不同的值.什么原因?

c++ stdio

0
推荐指数
1
解决办法
117
查看次数

C温度转换程序保持输出0华氏温度至摄氏温度

当我尝试将华氏温度转换为摄氏温度时,我在C中的温度转换程序保持输出0.从摄氏温度到华氏温度的转换似乎工作正常.对于函数和部分我都做了完全相同的事情但是第二次转换时我一直得到0.有人可以帮助我或告诉我我做错了什么?

#include <stdio.h>

//Function Declarations

float get_Celsius (float* Celsius);       //Gets the Celsius value to be converted.
void to_Fahrenheit (float cel);           //Converts the Celsius value to Fahrenheit and prints   the new value.
float get_Fahrenheit (float* Fahrenheit); //Gets the Fahrenheit value to be converted.
void to_Celsius (float fah);              //Converts the Fahrenheit value to Celsius and prints the new value.

int main (void)
{
   //Local Declarations
   float Fahrenheit;
   float Celsius;
   float a;
   float b;

   //Statements
   printf("Please enter a temperature value in Celsius to …
Run Code Online (Sandbox Code Playgroud)

c stdio temperature output

0
推荐指数
1
解决办法
2135
查看次数

编写原型而不是#include <stdio.h>

例如,这里是一个"hello,world"程序,不stdio.h包括:

int puts(const char *str);

int main(void)
{
    puts("hello, world");
}
Run Code Online (Sandbox Code Playgroud)

我甚至认为,当我的程序变得越来越长时,这可能是一种很好的编程风格,因为所有被调用的函数都是明确列出的.

所以我的问题是:除了为标准库函数提供原型外,还有#include <stdio.h>什么办法呢?

c stdio language-lawyer

0
推荐指数
2
解决办法
138
查看次数

如何在/usr/include/stdio.h中使用"#define stdin stdin"?

在/usr/include/stdio.h中

/* C89/C99 say they're macros.  Make them happy.  */
#define stdin stdin
#define stdout stdout
#define stderr stderr
Run Code Online (Sandbox Code Playgroud)

这应该怎么样?

c stdio

0
推荐指数
1
解决办法
189
查看次数