Tag: io

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()。 我能做些什么才能得到我期望的行为?

ERROR_IO_PENDING后的多个WriteFile

在我的应用程序中,当另一个Write操作在命名管道上挂起时,我正在测试WriteFile的行为。 管道处于消息模式(不是字节模式)。 为了使写入操作挂起,我保持命名管道的缓冲区非常小,并且客户端发送的数据多于缓冲区大小。 通过这种方式,我在客户端获得挂起的写操作。 我在服务器端面临以下问题: 使用ERROR_MORE_DATA失败ReadFile 。 它会更改缓冲区的内容,但不会更改接收的字节数。 服务器代码 //file server.cpp #include #include #include #include #define CONNECTING_STATE 0 #define READING_STATE 1 #define WRITING_STATE 2 #define INSTANCES 4 #define PIPE_TIMEOUT 5000 #define BUFSIZE 3 #define BUFSIZE2 1000 typedef struct { OVERLAPPED oOverlap; HANDLE hPipeInst; TCHAR chRequest[BUFSIZE2]; DWORD cbRead; DWORD cbReadSoFar; TCHAR chReply[BUFSIZE2]; DWORD cbToWrite; DWORD dwState; […]

逐字逐行读取文件

我有一个文件,每行有两个未知长度,用空格分隔。 数字可以是任何大小,最多500个数字,所以我需要逐个字符地读取它并将其存储到整数数组中。 我试过这样的事情: while(fgets(line, sizeof line, fp) != NULL) { // assuming buff handles the line lenght //read each character until ” // store it //read each character until ‘\n’ // store it } 我尝试使用fgetc读取直到”和’\ n’,但它没有用。 任何建议将不胜感激。

从二进制文件读取double(字节顺序?)

我有一个二进制文件,我想从中读取一个double。 在hex表示中,我在一个文件中有这8个字节(之后还有一些): 40 28 25 c8 9b 77 27 c9 40 28 98 8a 8b 80 2b d5 40 … 这应该对应于大约10的双倍值(基于该条目的含义)。 我用过 #include #include int main(int argc, char ** argv) { FILE * f = fopen(argv[1], “rb”); assert(f != NULL); double a; fread(&a, sizeof(a), 1, f); printf(“value: %f\n”, a); } 但是,打印值:-261668255698743527401808385063734961309220864.000000 很明显,字节不能正确转换为double。 到底是怎么回事? 使用ftell,我可以确认正在读取8个字节。

getchar()在C中给出输出

该计划应该做什么, #include main() { getchar(); } 我希望它能显示一个空白屏幕,直到我敲击键盘上的任何字符。 但它的作用很奇怪。 它会显示我按下的内容。 它永远不会终止,直到我按Enter键。 据我所知,getchar()应该只读一个字符。 它不应该输出任何东西。 为什么要打印我输入的每个字符? 编辑: 为什么getchar()在读取一个字符后停止,例如在此代码中: #include main() { getchar(); printf(“Done”); } 阅读一个字符后,程序应打印完成。

C getchar()不等待输入/条件循环不int

我正在尝试向我的C控制台应用程序计算器添加一个function,提示用户决定是否要使用: y或n执行另一个计算,但在测试中, getchar()拒绝等待输入并且程序继续进行,就像它收到了有效的输入。 以下是该function的最小示例: main() { char newCalculation; do{ lengthFormula(); /* main calculation formula */ printf(“Would you like to do another calculation? (Y/N)”); newCalculation = getchar(); }while(tolower( newCalculation ) == ‘y’); if(tolower(newCalculation) == ‘n’){ exitProgram(); /* exit the program */ } while(tolower(newCalculation) != ‘n’ && tolower(newCalculation) != ‘y’){ printf(“This is not a valid response.\n Please […]

scanf()调用之后的指令被调用两次

以下程序: #include #include char input; void * dpy(void *args) { printf(“\n[input] = %c\n”, input); } void * read(void *args) { pthread_t child; printf(“Write whatever – press ‘F’ to end\n”); do { scanf(“%c”, &input); printf(“begin\n”); pthread_create(&child, NULL, dpy, NULL); pthread_join(child, NULL); printf(“end\n”); } while (input!=’F’); printf(“done\n”); } void main () { pthread_t parent; pthread_create(&parent, NULL, read, […]

来自文本文件的C字符串

我是C编程语言的新手。 如何从文本文件中搜索特定字符串并将其转换为数组,然后从这些数组中生成单个字符串? 文本文件: name1,name2,name3,type1,g1,g2,g3 name1,last1,last2,type2,g4,g6,g7 foo1,foo2,foo3,type3,gx,g3,g5 foo1,doo1,doo2,type4,g1,gt,gl 输出应该是1个字符串,不分开,所以如果让我们说它是 printf(“%s”, strings); 它给出了如下输出: 2 records found Name: name1,name2,name3 type: type1 g1 type: g1 g2 type: g2 g3 type: g3 Name: name1,last1,last2 type: type2 g1 type: g4 g2 type: g6 g3 type: g7 到目前为止,我的尝试是获取文本文件并搜索字符串: #include #include #include int main(){ char tmp[1000]; int count=0; FILE *fp=fopen(“test.csv”,”r”); while(fgets(tmp, sizeof(tmp),fp) != […]

如何从文件中获取替代行并将其作为字符串存储到结构中?

我有一个文件需要通过代码读取。 该文件如下所示。 文件的第一行包含一个整数,表示文件中的日记条目数。 我需要编写一个C程序来读取文件并将内容存储在动态分配的结构数组中。 4 12/04/2010 Interview went well I think, though was told to wear shoes. 18/04/2010 Doc advised me to concentrate on something… I forget. 03/05/2010 Was asked today if I was an art exhibit. 19/05/2010 Apparently mudcakes not made of mud, or angry wasps. 我能够strtok()将日期,月份和年份存储在我的结构中,但是我坚持将字符串保存到我的结构中。 这是我的strtok()代码, FILE* file=fopen(“struct.txt”,”r”); if (file==NULL){ perror(“Error opening […]

用C ++写一个文件的不同结构?

我需要一种方法将三种不同类型的结构写入二进制文件,以后必须进行搜索。 (例如,结构A有两个字段,一个是int和一个char; struct B有一个int和一个long;我需要输出所有结构,其int等于键盘给出的结构)。 我理解如何在文件中编写相同类型的结构以及如何搜索它们,但在这里我只是迷失了,我想出的最好的事情是声明一个包含所有可能需要的字段的结构并留下我不需要的字段空的,但它确实感觉不对,有一个更好的方法来做到这一点。 我读过关于二进制文件的内容并且找不到任何相关内容,大多数示例和教程都涉及编写一种数据类型。 有人能指出我正确的方向吗? 编辑:我正在寻找@Jerry_coffin所谓的数据库模式,可能会使用现有的数据库系统之一,最好的方法。 谢谢大家的建议