fgets

How to read non-ASCII characters from CLI standard input

为君一笑 提交于 2019-12-02 20:44:57
If I type å in CMD, fgets stop waiting for more input and the loop runs until I press ctrl-c . If I type a "normal" characters like a-z0-9!?() it works as expected. I run the code in CMD under Windows 7 with UTF-8 as charset ( chcp 65001 ), the file is saved as UTF-8 without bom. I use PHP 5.3.5 (cli). <?php echo "ÅÄÖåäö work here.\n"; while(1) { echo '> '. fgets(STDIN); } ?> If I change charset to chcp 1252 the loop doesn't break when I type å and it print "> å" but the "ÅÄÖåäö work here" become "ÅÄÖåäö work here!". And I know that I can change the file to ANSI, but then I can't use

using fgets and strcmp in C [duplicate]

拟墨画扇 提交于 2019-12-02 20:13:23
问题 This question already has answers here : strcmp on a line read with fgets (6 answers) Closed 6 years ago . I'm trying to get a string input from the user and then run different functions depending on the input they've entered. For example, say I asked, "What is your favorite fruit?" and I want the program to comment depending on what they enter...I'm not sure how to do this. Here's what I have so far: #include <stdio.h> #include <string.h> char fruit[100]; main() { printf("What is your

Seg Fault error reading from file with fgets

别说谁变了你拦得住时间么 提交于 2019-12-02 17:59:20
问题 //declare double pointer so that create array can "return" an array int **aryReturn; int size; char trashdata[100]; //open file FILE *inFilePtr = fopen(*(argv + 1), "r" ); if (inFilePtr != NULL) printf(" the value of argv 1 is %s \n", argv[1]); while (fgets(trashdata, sizeof(int) * 10, inFilePtr) != NULL){ fgets(trashdata, 10, inFilePtr); size++; } can anyone tell me why my loop condition will not work! I get a seg fault that says fp(0x0) at fgets. I have tried while (!feof(inFilePtr)) And I

Fgets errors seg fault

你离开我真会死。 提交于 2019-12-02 14:23:28
问题 Is there any reason that a program, which compiled earlier, should seg fault at a point because of fgets? I changed no code related to it AT ALL. Suddenly I believe it is failing to open the file, but I tested it with the file like fifteen minutes ago.... All I did was add a search function, so I don't understand what the issue is..... Could it be the server I'm connecting to over PuTTy? int createarray( int **arrayRef, FILE *fptr){ int size = 0, i; char rawdata[100]; while (fgets(rawdata, 99

fgets() includes new line in string

折月煮酒 提交于 2019-12-02 13:27:19
I get the words from my document extracted and all are printed on screen, but after each word printed there is a blank line. How can I avoid reading or adding this new line to the string? int main(void) { FILE *f; f = ("words", "r"); char string[100]; while (fgets(string, 100, f)) { printf("%s", string); } } This code was not copy pasted, so I could have forgotten tiny pieces but should work. In words.txt I have one word on each line. My program prints them all to screen, but adds a new line after each word. I do not want it to add a new line, or a space. So if the txt had Hello on one line

using fgets and strcmp in C [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-02 11:11:19
This question already has an answer here: strcmp on a line read with fgets 6 answers I'm trying to get a string input from the user and then run different functions depending on the input they've entered. For example, say I asked, "What is your favorite fruit?" and I want the program to comment depending on what they enter...I'm not sure how to do this. Here's what I have so far: #include <stdio.h> #include <string.h> char fruit[100]; main() { printf("What is your favorite fruit?\n"); fgets (fruit, 100, stdin); if (strcmp(fruit, "apple")) { printf("Watch out for worms!\n"); } else { printf(

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

血红的双手。 提交于 2019-12-02 10:07:17
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 () { char* token; char str[LINE_LEN]; char* arr[MAX_PARTS]; int i,j; bool go_on = true; while (go_on ==

Fgets errors seg fault

橙三吉。 提交于 2019-12-02 09:48:17
Is there any reason that a program, which compiled earlier, should seg fault at a point because of fgets? I changed no code related to it AT ALL. Suddenly I believe it is failing to open the file, but I tested it with the file like fifteen minutes ago.... All I did was add a search function, so I don't understand what the issue is..... Could it be the server I'm connecting to over PuTTy? int createarray( int **arrayRef, FILE *fptr){ int size = 0, i; char rawdata[100]; while (fgets(rawdata, 99, fptr) != NULL){ size++; } rewind(fptr); *arrayRef = malloc(sizeof(int) * size); for ( i = 0; i < size

How to use fgets to read a file line by line

别来无恙 提交于 2019-12-02 09:36:54
I'm new at programming so there are some basics and maybe common sense that I don't know. I have a question about how to use fgets right. Based on the explanation of fgets, it seems that fgets should stop whenever it reads n-1 characters, hit the EOF or hit a newline character. For example, I create a text file like below: red 100 yellow 400 blue 300 green 500 purple 1000 ... The color and the integer is separated by a tab. When I create this text file, I need to hit enter at the end of each line to start a new line. In this case, hitting enter equals to add a newline character, '\n', is that

Seg Fault error reading from file with fgets

你说的曾经没有我的故事 提交于 2019-12-02 08:37:20
//declare double pointer so that create array can "return" an array int **aryReturn; int size; char trashdata[100]; //open file FILE *inFilePtr = fopen(*(argv + 1), "r" ); if (inFilePtr != NULL) printf(" the value of argv 1 is %s \n", argv[1]); while (fgets(trashdata, sizeof(int) * 10, inFilePtr) != NULL){ fgets(trashdata, 10, inFilePtr); size++; } can anyone tell me why my loop condition will not work! I get a seg fault that says fp(0x0) at fgets. I have tried while (!feof(inFilePtr)) And I basically get the same error, but it says feof is the problem. My file seems to open correctly because