fgets

Read a line of input faster than fgets?

喜欢而已 提交于 2019-11-30 09:05:20
I'm writing a program where performance is quite important, but not critical. Currently I am reading in text from a FILE* line by line and I use fgets to obtain each line. After using some performance tools, I've found that 20% to 30% of the time my application is running, it is inside fgets . Are there faster ways to get a line of text? My application is single-threaded with no intentions to use multiple threads. Input could be from stdin or from a file. Thanks in advance. You don't say which platform you are on, but if it is UNIX-like, then you may want to try the read() system call, which

Usage of fgets function in C

浪子不回头ぞ 提交于 2019-11-30 09:02:43
问题 One of my assignments in to write my own UNIX Shell. To receive input from the user, I am using fgets to capture the input as a string but I'm not really sure how it works. When I run: char command[50]; fgets(command, sizeof(command), stdin); printf("Your Command: %s", &command); int length = strlen(command); printf("Length of String: %d\n", length); Lets say my the input was "exit". strlen says that the string is 5 characters long, instead of four. I want to do this: if( (strcmp(command,

fgets() not working after fscanf()

坚强是说给别人听的谎言 提交于 2019-11-29 16:02:37
I am using fscanf to read in the date and then fgets to read the note. However after the first iteration, fscanf returns a value of -1. I used GDB to debug the program step by step. It works fine until the first use of fgets. When I try print out the line read by fgets on the first iteration, it gives me this: (gdb) print line $6 = "\rtest\r18/04/2010\rtest2\r03/05/2010\rtest3\r05/08/2009\rtest4\r\n\000\000\000\000q\352\261\a\370\366\377\267.N=\366\000\000\000\000\003\000\000\000\370xC\000\000\000\000\000\000\000\000\000\001\000\000\000\227\b\000\000\070\367\377\267H\364\377\267\362\202\004

Why do I get an assertion failure?

谁都会走 提交于 2019-11-29 15:34:55
This code fails when I try to debug it using VC2010: char frd[32]="word-list.txt"; FILE *rd=fopen(frd,"r"); if(rd==NULL) { std::cout<<"Coudn't open file\t"<<frd; exit(1); } char readLine[100]; while(fgets(readLine, 100, rd) != NULL) { readLine[strlen(readLine) - 1] = '\0'; char *token = NULL; token = strtok(readLine, " ,"); insert(readLine); } Debugging results in --------------------------- Microsoft Visual C++ Debug Library----------- Debug Assertion Failed! Program: ...\documents\visual studio 2010\Projects\bfa\Debug\bfa.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\fgets.c Line: 57

Read unknown number of lines from stdin, C

和自甴很熟 提交于 2019-11-29 12:39:01
i have a problem with reading stdin of unknown size. In fact its a table in .txt file, which i get to stdin by calling parameter '<'table.txt. My code should look like this: #include <stdio.h> #include <string.h> int main(int argc,char *argv[]) { char words[10][1024]; int i=0; while(feof(stdin)==0) { fgets(words[i],100,stdin); printf("%s", words[i]); i++; } return 0; } but there is the problem i dont know the nuber of lines, which in this case is 10(we know the number of characters in line - 1024). It would be great if someone know the solution. Thanks in advance. You have hit on one of the

reading a block of lines in a file using php

徘徊边缘 提交于 2019-11-29 12:13:06
Considering i have a 100GB txt file containing millions of lines of text. How could i read this text file by block of lines using PHP? i can't use file_get_contents(); because the file is too large. fgets() also read the text line by line which will likely takes longer time to finish reading the whole file. If i'll be using fread($fp,5030) wherein '5030' is some length value for which it has to read. Would there be a case where it won't read the whole line(such as stop at the middle of the line) because it has reached the max length? i can't use file_get_contents(); because the file is too

Usage of fgets function in C

泪湿孤枕 提交于 2019-11-29 11:28:51
One of my assignments in to write my own UNIX Shell. To receive input from the user, I am using fgets to capture the input as a string but I'm not really sure how it works. When I run: char command[50]; fgets(command, sizeof(command), stdin); printf("Your Command: %s", &command); int length = strlen(command); printf("Length of String: %d\n", length); Lets say my the input was "exit". strlen says that the string is 5 characters long, instead of four. I want to do this: if( (strcmp(command, "exit")) == 0 ){ doSomething(); } but command is never equaling the string that I want it to; its like it

fgets skip the blank line

和自甴很熟 提交于 2019-11-29 08:05:54
I am writing a C program that use fgets to read in each line from a file. The problem is that if the file have a blank line, how to skip it to get the next line ? This is what I had try so far but it did not work. char line[100]; FILE *filePtr = fopen(filename, "r"); while(fgets(line, sizeof(line), filePtr) != NULL) //read each line of the file { if (line != "\n") { //do something } else { continue; } } Change if (line != "\n") into if (line[0] != '\n') You can also use the strcmp function to check for newline //Check for dos and unix EOL format if(strcmp(line,"\n") || strcmp(line,"\r\n")) { /

C fgets versus fgetc for reading line

社会主义新天地 提交于 2019-11-29 07:48:33
问题 I need to read a line of text (terminated by a newline) without making assumptions about the length. So I now face to possibilities: Use fgets and check each time if the last character is a newline and continuously append to a buffer Read each character using fgetc and occasionally realloc the buffer Intuition tells me the fgetc variant might be slower, but then again I don't see how fgets can do it without examining every character (also my intuition isn't always that good). The lines are

Using fgets(char* c, int i, file* f) with printf() for C in Eclipse - CDT. The order of output is not correct.!

杀马特。学长 韩版系。学妹 提交于 2019-11-28 14:14:14
#include <stdio.h> enum { max_string = 127 }; static char ch[max_string+1] = ""; int main(int argc, char ** argv){ printf("Type a String: \n"); fgets(ch, max_string, stdin); printf("the string is: %s", ch); return 0; } I have used this code and the output in the console was hello world Type a String: the string is: hello world 'hello world' is the input which I give. My question is why isn't the order not maintained in this case. As printf() should work before fgets(), but here it isn't that way. I have checked with the same compiler in Code::Blocks. There it works in order. But in case of