fgets

C - Replacing words

与世无争的帅哥 提交于 2019-12-10 12:35:02
问题 My goal here is to read text from a file redirected from stdin, then replace certain argv passed words with the word "Replaced". For example, if I run: $ ./a.exe line < input.txt where input.txt is "Test line one", at the end I should print "Test Replaced one." I'm not quite sure where my code is going wrong, sometimes I get segmentation fault, and I'm also not sure how I would go about printing the newOut string, or if I even need one. As a side note, if I was reading using fgets, what if

How to implement gets() or fgets() while using structure pointer?

半世苍凉 提交于 2019-12-10 11:38:18
问题 The below code runs perfectly with scanf() , but i want to input characters along with white space. I have tried gets() and fgets() (commented below), but its not working (its skipping to next iteration in the loop & displaying blank(NULL) during output for Name input used by fgets() ). Any idea how to do it? PS: I've tried fputs() with sample programs, its working fine. But i am little confused while using structure pointer. #include <stdio.h> #include <stdlib.h> struct details { uint Id;

When I run my program, fgets() is ignored and scanf() doesn't input data to structure

左心房为你撑大大i 提交于 2019-12-10 11:21:19
问题 Whe I run it fgets() is simply ignored and scanf() doesn't input data to structure. My code: #include"stdio.h" #include"stdlib.h" typedef struct { char nazv[50]; int numr; int date[3]; int time[2]; } train; void input(train*rasp,int n); void print(train*rasp,int n); void output(train*rasp, int n, int*date, int*time); main() { int n; int t[2]; int d[3]; printf("How much? "); scanf("%d",&n); train* rasp=(train*)malloc(n*sizeof(train)); input(rasp,n); printf("Enter date: "); for (int date=0;date

C structure not scanning all the inputs

一曲冷凌霜 提交于 2019-12-10 11:18:18
问题 I have this C code: #include "stdio.h" main() { struct books { char name[100],author[100]; int year,copies; }book1,book2; printf("Enter details of first book\n"); gets(book1.name); gets(book1.author); scanf("%d%d",&book1.year,&book1.copies); printf("Enter details for second book\n"); gets(book2.name); gets(book2.author); scanf("%d%d",&book2.year,&book2.copies); printf("%s\n%s\n%d\n%d\n",book1.name,book1.author,book1.year,book1.copies); printf("%s\n%s\n%d\n%d\n",book2.name,book2.author,book2

fgets()

孤街浪徒 提交于 2019-12-10 08:27:17
在写网络编程时候遇到一个问题:通过fgets读取到了一行输入到缓冲区中,总是要通过strlen()来查下缓冲区中的长度,然后替换。 一开始没懂这个操作,后来查了下资料,原来fgets在读取输入流的时候,会读取你最后的那个回车,也就是'\n'。 比如你现在输入:abcde 实际上保存到缓存区中的是:abcde\n 然而fgets()会自动再补一个‘\0’,也就是说保存到缓冲区send_line中的是abcde\n\0,但是其实我们就没想要这个'\n'这势必会造成数据的不准确,所以我们就要自己来进行一个替换,把最后的'\n'给换成'\0'。 fgets(char *s, int size, FILE *stream) //s 缓冲区 //size 最大读取长度 //文件流 如 stdin while (fgets(send_line, MAXLINE, stdin) != NULL) { int i = strlen(send_line); if (send_line[i - 1] == '\n') { send_line[i - 1] = 0; } ... } 这里还有个疑问,既然都会保存'\n',那为什么要判断呢?直接把最后一个'\n'进行替换不就好了吗? 我试着输入超过给定size大小的字符串,比如: 就发现,当你输入超过size的时候,其实不会再保存那个'\n'换行符了

How to read and overwrite text file in C?

旧街凉风 提交于 2019-12-10 04:12:02
问题 I have a text file text.txt that reads (for simplicity purposes) this is line one this is line two this is line three Again for simplicity's sake, I am just trying to set the first character in each line to 'x', so my desired result would be xhis is line one xhis is line two xhis is line three So I am opening the text.txt file and trying to overwrite each line with the desired output to the same text file. In the while loop, I set the first character in each line to 'x'. I also set the

c segmentation fault fgets

萝らか妹 提交于 2019-12-09 03:46:34
问题 int main( int argc, char** argv) { FILE *inFilePtr = fopen(*(argv + 1), "r"); char *rawdata = malloc(sizeof(char) * 100); float *ary = malloc(sizeof(float) * 50); int counter = 0; float averageAns; int count = 0; while (count < 1 ){ //fgets(rawdata, 50, inFilePtr); //I have tried both fscanf(inFilePtr, "%s", rawdata); *(ary + counter) = atof(strtok(rawdata, ",")); counter++; *(ary + counter ) = atof(strtok(rawdata, NULL)); counter++; *(ary + counter) = atof(strtok(rawdata, NULL)); counter++;

First fgets() call being skipped during input?

删除回忆录丶 提交于 2019-12-08 04:23:24
问题 I'm writing a program that can take computer specifics from the user in manual mode. However, I've run into a bit of a problem. In this code: char choice = getc(stdin); if (choice == 'y' || choice == 'Y') { config_file = fopen(config_file_loc, "w"); printf("%s", "Please enter the name of your distribution/OS: "); fgets(distro_str, MAX_STRLEN, stdin); fputs(distro_str, config_file); fputs("\n", config_file); printf("%s", "Please enter your architecture: "); fgets(arch_str, MAX_STRLEN, stdin);

Php Save fopen Result To A String

南笙酒味 提交于 2019-12-08 02:49:37
问题 How can I get the "$buffer" value into a string, and use it outside the fopen and fclose functions? Thanks. $handle = @fopen("http://www.example.com/", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgetss($handle, 5000); echo $buffer ; } fclose($handle); } 回答1: $handle = @fopen("http://www.example.com/", "r"); $buffer = ''; if ($handle) { while (!feof($handle)) { $buffer .= fgetss($handle, 5000); } fclose($handle); } 回答2: Try file_get_contents() : $buffer = file_get_contents("/my

How to remove extra characters input from fgets in C?

放肆的年华 提交于 2019-12-08 02:08:22
问题 I heard using gets() is bad in C programming and it's safer using fgets... So I am using fgets. However, I encounter a problem with fgets: I entered too much characters and somehow, it overflows. How to get rid of the extra input characters? char answer[4]; char answer2[4]; fgets(answer,sizeof(answer),stdin); printf("answer: %s\n",answer); fgets(answer2,sizeof(answer2),stdin); printf("answer2: %s\n",answer2); For example, for the first fgets, I enter 123456, the output I get is answer: 123