fgets

Trouble creating a shell in C (Seg-Fault and ferror)

我的梦境 提交于 2020-01-16 05:14:16
问题 I have been following a tutorial on how to make my own shell but I have been stuck for a couple days now. Two things: When this code is compiled and ran, it will randomly have segmentation faults and I cannot figure out why. The if statement `if (ferror != 0)` always seems to be true. which is odd because I do not understand why fgets() is failing in the main() function. Any information on these topics (or other topics about creating this shell) would be greatly appreciated. #include <stdio.h

Reading the file with comma delimiter using fgets() and strtok()

不羁岁月 提交于 2020-01-15 23:37:25
问题 I have a text file with three fields separated by comma. Example of the content of my text file: 12345, true programming newbie, BS ME To load the file into the program, i used the below code.... my problem is that sometimes the code works and sometimes it doesn't (no error message appears the program just closes itself and doesn't continue). i also observed the text file is blank (nothing is written) it automatically closes itself and doesn't continue. Your help would be highly appreciated.

How to read a date from file in c

℡╲_俬逩灬. 提交于 2020-01-14 15:57:14
问题 I am reading from a file in a program using C. in the file I have some dates each in a separate line exactly like this: 20190101 20190304 20180922 Now I want the program to read these as dates and find the difference between the current date and these dates. is it possible to convert these dates into a format which is readable by C? something like this: 2019.01.01 Currently, I read it using fgets and I am not able to convert it to the format above. This is my code: #include <stdio.h> #include

How to use fgets if you don't know the number of characters to be read?

筅森魡賤 提交于 2020-01-12 05:11:09
问题 I need to read a file and send the text from it to a string so I can parse it. However, the program won't know exactly how long the file is, so what would I do if I wanted to use fgets() , or is there a better alternative? Note: char *fgets(char *str, size_t num, FILE *stream); 回答1: Don't forget that fgets() reads a line at a time, subject to having enough space. Humans seldom write lines longer than ... 80, 256, pick a number ... characters. POSIX suggests a line length of 4096. So, I

How to use feof and ferror for fgets (minishell in C)

独自空忆成欢 提交于 2020-01-11 12:45:28
问题 I've written this minishell but I'm not sure I'm making a correct control of the errors. I know fgets can return feof and ferror (http://www.manpagez.com/man/3/fgets/) but I don't know how to use them. I've checked if fgets returns a null pointer (which indicates the content of the buffer is inditerminate) but i would like to know how to use feof and ferror. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #define LINE_LEN 50 #define MAX_PARTS 50 int main () {

How to use feof and ferror for fgets (minishell in C)

岁酱吖の 提交于 2020-01-11 12:45:13
问题 I've written this minishell but I'm not sure I'm making a correct control of the errors. I know fgets can return feof and ferror (http://www.manpagez.com/man/3/fgets/) but I don't know how to use them. I've checked if fgets returns a null pointer (which indicates the content of the buffer is inditerminate) but i would like to know how to use feof and ferror. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #define LINE_LEN 50 #define MAX_PARTS 50 int main () {

Prevent buffer overflows with gets [duplicate]

ε祈祈猫儿з 提交于 2020-01-09 11:39:10
问题 This question already has answers here : Why is the gets function so dangerous that it should not be used? (11 answers) Closed 2 years ago . The declaration of gets is: char * gets ( char * str ); Note the glaring omission of a maximum size for str . cplusplus.com says 2 : Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str ( which

C read entire line of file [closed]

一世执手 提交于 2020-01-07 06:36:50
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I am trying to program a tool in C . Part of this program is to use a text file and read it line by line, while storing all lines into an array to have it available for future use. That's what I have so far: int main(){ FILE *fp = fopen("file.txt", "ab+"); if (fp == NULL) { printf("FILE ERROR"); return 1; } int

Safely reading in strings of unknown length

牧云@^-^@ 提交于 2020-01-06 14:58:09
问题 I am trying to safely get in strings of unknown length with using fgets This is the code I have been able to come up with so far, but now I am stuck on how to conitinue forward. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define DEFLEN 4 #define CHUNKSZ 2 int i = 0; int size =0; int size2 =0; char* ln1; char* ln2; FILE *fpin; char *getStrFromFile( FILE *fpins )/*file stream to read string from */ { DEFLEN = malloc( CHUNKSZ *sizeof(int)); while(1){ if( fgets

Why fgets is not inputting first value?

邮差的信 提交于 2020-01-06 13:53:14
问题 I am writing a program to write my html files rapidly. And when I came to write the content of my page I got a problem. #include<stdio.h> int main() { int track; int question_no; printf("\nHow many questions?\t"); scanf("%d",&question_no); char question[question_no][100]; for(track=1;track<=question_no;track++) { printf("\n<div class=\"question\">%d. ",track); printf("\nQuestion number %d.\t",track); fgets(question[track-1],sizeof(question[track-1]),stdin); printf("\n\n\tQ%d. %s </div>",track