fgets

C 输入 & 输出

心已入冬 提交于 2019-12-22 14:59:33
C 输入 & 输出 当我们提到 输入 时,这意味着要向程序填充一些数据。输入可以是以文件的形式或从命令行中进行。C 语言提供了一系列内置的函数来读取给定的输入,并根据需要填充到程序中。 当我们提到 输出 时,这意味着要在屏幕上、打印机上或任意文件中显示一些数据。C 语言提供了一系列内置的函数来输出数据到计算机屏幕上和保存数据到文本文件或二进制文件中。 标准文件 C 语言把所有的设备都当作文件。所以设备(比如显示器)被处理的方式与文件相同。以下三个文件会在程序执行时自动打开,以便访问键盘和屏幕。 标准文件 文件指针 设备 标准输入 stdin 键盘 标准输出 stdout 屏幕 标准错误 stderr 您的屏幕 文件指针是访问文件的方式,本节将讲解如何从屏幕读取值以及如何把结果输出到屏幕上。 C 语言中的 I/O (输入/输出) 通常使用 printf() 和 scanf() 两个函数。 scanf() 函数用于从标准输入(键盘)读取并格式化, printf() 函数发送格式化输出到标准输出(屏幕)。 实例 #include <stdio.h> // 执行 printf() 函数需要该库 int main() { printf("菜鸟教程"); //显示引号中的内容 return 0; } 编译以上程序,输出结果为: 菜鸟教程 实例解析: 所有的 C 语言程序都需要包含 main()

C: Reading a text file (with variable-length lines) line-by-line using fread()/fgets() instead of fgetc() (block I/O vs. character I/O)

烈酒焚心 提交于 2019-12-22 05:23:49
问题 Is there a getline function that uses fread (block I/O) instead of fgetc (character I/O)? There's a performance penalty to reading a file character by character via fgetc . We think that to improve performance, we can use block reads via fread in the inner loop of getline . However, this introduces the potentially undesirable effect of reading past the end of a line. At the least, this would require the implementation of getline to keep track of the "unread" part of the file, which requires

Why do i have to input EOF 3 times when using fgets?

喜夏-厌秋 提交于 2019-12-21 21:37:02
问题 So basically I want to copy everything i write to stdin (including newline char) to string for hash purposes. I managed to accomplish that and made small code to represent my problem. #include <stdio.h> #include <string.h> #include <stdlib.h> #define BUFFERSIZE 10000 int main() { char *myStr = calloc(1,1); char buffer[BUFFERSIZE]; while( fgets(buffer, BUFFERSIZE , stdin) != NULL ){ myStr = realloc(myStr, strlen(myStr)+1+strlen(buffer) ); strcat( myStr, buffer ); } printf("\n%s\n",myStr); }

Segmentation fault in fgets() - C language

谁都会走 提交于 2019-12-21 05:56:40
问题 I am getting a segmentation fault exactly at this line: while (fgets(line, MAX_LEN + 1, stream) != NULL) { .... } where MAX_LEN is 500, line is reading the current line, and stream is open through fopen(filename, "r"); I am reading lines from a file with a specific format and I get a segmentation fault (Core dumps) exactly at this line according to the debugger. I made my code so that it ignores lines that do not match the scanf format. Here is what I am implementing. Something close to it at

Write an EOF to a pipe

一个人想着一个人 提交于 2019-12-20 05:21:54
问题 I have a parent and a child prozess and want to write an EOF from the parent to the child via a pipe... how does this work? here is my attampt: ---parent code--- if(dup2(parent_pipe[1], STDOUT_FILENO) == -1) { /*stdout gets closed and parent_pipe is duplicated with id of stdout*/ error("Can't duplicate the write-end of the pipe to stdout!"); } if(close(parent_pipe[0]) == -1) { error("Can't close read-end of the pipe!"); } char blub[] = "EOF"; if(write(parent_pipe[1], blub, strlen(blub)) == -1

How fget works?

空扰寡人 提交于 2019-12-20 04:35:11
问题 I am using gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2 I am writing a very simple script to take string as input and print the same with the some custom message. First user enters the T(no. of times to take string) and then takes input by fgets . . I used this&this as reference. I am getting a very strange output ie fgets is adding some extra new lines and even loop is not working properly for T=2 it is asking input only once. Can anybody explain me what is wrong with snippet. Thanks in advance!

Fgets skipping inputs [duplicate]

橙三吉。 提交于 2019-12-20 01:13:34
问题 This question already has answers here : fgets doesn't work after scanf (7 answers) Closed 5 years ago . I've tried looking around and I can't seem to find where the error lies. I know it must have something to do with the way I used fgets but I can't figure out for the life of me what it is. I've read that mixing fgets and scanf can produce errors so I've even changed my second scanf to fgets and it still skips the rest of my inputs and only prints the first. int addstudents = 1; char name

How to use fgets properly in a structure?

放肆的年华 提交于 2019-12-19 19:46:31
问题 I can't work out what's the problem with my code. Here's my code: #include <stdio.h> #include <stdlib.h> #define N 20 typedef struct _dog { char dogName[N],ownerName[N]; int dogAge; } Dog; int main() { //Dynamic array int size; printf("Number of dogs: "); scanf("%d", &size); Dog *dog = (Dog*)malloc(sizeof(Dog)*size); printf("\n"); //Input int i; printf("Please provide the data: [dogName][ownerName][dogAge] :\n"); for(i=0;i<size;i++) { fgets(dog[i].dogName, sizeof(dog[i].dogName), stdin);

fgets and dealing with CTRL+D input

时光毁灭记忆、已成空白 提交于 2019-12-19 09:47:59
问题 I am grabbing some standard input from the user and if the user presses CTRL+D , I want to display an error and terminate the program. I think perhaps my issue may be related to being stuck in a while loop; int readInput(){ char buff[10]; int count = 0; int counter; printf("Enter random number: "); fgets(buff, 10, stdin); if ((int) strtol(buff, NULL, 10) == 0){ printf("Error reading number. \n"); return 0; //This will get hit if the user presses CTRL+D at this input. } counter = atol(buff);

Reading and parsing lines from a file with fgets and strtok

元气小坏坏 提交于 2019-12-19 09:08:09
问题 I'm having trouble with a fairly basic bit of code. I need to read each line from the file shown below, split it up into the 3 parts with strtok, and store each part into an array. The arrays for "goals" and "assists" are working perfectly, but for some reason the entire name array is filled with the last name read from the file. Input file: Redden 2 0 Berglund 5 2 Jackman 2 0 Stewart 4 0 Oshie 3 5 McDonald 2 4 Pietrangelo 2 7 Perron 2 6 Tarasenko 5 5 Relevant code: int main(int argc, char*