stdio

Turn simple C program into server using netcat

我与影子孤独终老i 提交于 2020-01-22 21:23:29
问题 One cool feature of netcat is how it can turn any command line program into a server. For example, on Unix systems we can make a simple date server by passing the date binary to netcat so that it's stdout is sent through the socket: netcat -l -p 2020 -e date Then we can invoke this service from another machine by simply issuing the command: netcat <ip-address> 2020 Even a shell could be connected ( /bin/sh ), although I know this is highly unrecommended and has big security implications.

ios_base::sync_with_stdio(false) does not work between two inputs from stdin

喜欢而已 提交于 2020-01-17 05:23:27
问题 I am wondering why does the following code not work as expected: #include <iostream> #include <string> using namespace std; int main(){ int n; string s; //scanf("%d",&n); cin >> n; ios_base::sync_with_stdio(false); cin >> s; cout << n << " " << s; return 0; } Input: 10 abcd Output: 10 The string is not getting printed! The result is same if I use scanf to input the integer. However, the code does work as expected if the line ios_base::sync_with_stdio(false); is placed before the first cin (or

Content written before fork() present in output twice

a 夏天 提交于 2020-01-15 11:24:53
问题 I wrote the following C code: #include<stdio.h> int main(){ printf("A"); if(fork() == 0){ printf("B"); } else{ printf("C"); } } The output I got is: ACAB I expected this code to print A only once. Can anyone explain this output? 回答1: Your error is not flushing the buffers before fork -ing, thus both processes will write it. Add this before fork() : fflush(0); // Flush all output-streams 回答2: The 'A' is stored in a buffer and flushed by both processes when they exit. 来源: https://stackoverflow

Content written before fork() present in output twice

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-15 11:24:30
问题 I wrote the following C code: #include<stdio.h> int main(){ printf("A"); if(fork() == 0){ printf("B"); } else{ printf("C"); } } The output I got is: ACAB I expected this code to print A only once. Can anyone explain this output? 回答1: Your error is not flushing the buffers before fork -ing, thus both processes will write it. Add this before fork() : fflush(0); // Flush all output-streams 回答2: The 'A' is stored in a buffer and flushed by both processes when they exit. 来源: https://stackoverflow

Binary output in Windows

亡梦爱人 提交于 2020-01-04 09:37:11
问题 I wrote a program that reads a binary file, does some process with its contents and writes the results to a different file. In Linux it works perfectly, but in Windows it does not work; the output files are always 1KB... This is a simplified version of the program: #include <stdio.h> void copyFile(char* source, char* dest); int main (int argc, char* argv[]) { if (argc != 3) printf ("usage: %s <source> <destination>", argv[0]); else { copyFile(argv[1], argv[2]); } } void encryptFile(char*

Binary output in Windows

拥有回忆 提交于 2020-01-04 09:37:09
问题 I wrote a program that reads a binary file, does some process with its contents and writes the results to a different file. In Linux it works perfectly, but in Windows it does not work; the output files are always 1KB... This is a simplified version of the program: #include <stdio.h> void copyFile(char* source, char* dest); int main (int argc, char* argv[]) { if (argc != 3) printf ("usage: %s <source> <destination>", argv[0]); else { copyFile(argv[1], argv[2]); } } void encryptFile(char*

Where can I find source code to to 'truly' understand what the standard functions are doing in stdio.h? [closed]

孤街浪徒 提交于 2020-01-02 18:25:43
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . As a newb, like myself, to have great difficulty with searching the header files such as stdio.h for a function like getchar().

How to name and create file in a loop

半腔热情 提交于 2020-01-01 15:32:42
问题 In C, I want to create and open text files to write data into, but the problem is I want to name the files on the go, such as FILE *ptr; for(i=0;i<1000;i++){ fopen_s(&ptr,"i.txt","w"); operations to fill data into file i.txt; fclose(ptr); } such that I will create file 0.txt, 1.txt, 2.txt ... 999.txt. How is this possible? I checked open and rename functions, but couldn't find a way to do. Thank you so much for all your help. Best, 回答1: use snprintf to set the file number: FILE *ptr; char

Is it ‘safe’ to remove() open file?

蓝咒 提交于 2020-01-01 12:32:09
问题 I think about adding possibility of using same the filename for both input and output file to my program, so that it will replace the input file. As the processed file may be quite large, I think that best solution would to be first open the file, then remove it and create a new one, i.e. like that: /* input == output in this case */ FILE *inf = fopen(input, "r"); remove(output); FILE *outf = fopen(output, "w"); (of course, with error handling added) I am aware that not all systems are going

how to read a string from a \n delimited file

匆匆过客 提交于 2019-12-30 18:39:13
问题 I'm trying to read a return delimited file. full of phrases. I'm trying to put each phrase into a string. The problem is that when I try to read the file with fscanf(file,"%50s\n",string); the string only contains one word. when it bumps with a space it stops reading the string 回答1: fscanf can be modified to read past spaces. The details are a bit complicated. Here is what the man page says about %[...] Matches a nonempty sequence of characters from the specified set of accepted characters;