How to read a line from stdin, blocking until the newline is found?
Try this patch char *line = NULL; +size_t size; +if (getline(&line, &size, stdin) == -1) { -if (getline(&line, 0, stdin) == -1) { printf(“No line\n”); } else {
Try this patch char *line = NULL; +size_t size; +if (getline(&line, &size, stdin) == -1) { -if (getline(&line, 0, stdin) == -1) { printf(“No line\n”); } else {
Each has its own overheads. Depending on what you print, either may be faster. Here are two points that come to mind – printf() has to parse the “format” string and act upon it, which adds a cost. cout has a more complex inheritance hierarchy and passes around objects. In practice, the difference shouldn’t matter … Read more
The documentation that you linked give the answer: The java.nio.file package defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems. This API may be used to overcome many of the limitations of the java.io.File class. The toPath method may be used to obtain a Path that uses … Read more
I think you can try: Stream<String> lines = new BufferedReader(new InputStreamReader(is, cs)).lines();
To read a text file with a specific encoding you can use a FileInputStream in conjunction with a InputStreamReader. The right Java encoding for Windows ANSI is Cp1252. reader = new BufferedReader(new InputStreamReader(new FileInputStream(csvFile), “Cp1252”)); To write a text file with a specific character encoding you can use a FileOutputStream together with a OutputStreamWriter. writer … Read more
You can use the csv module to parse tab seperated value files easily. import csv with open(“tab-separated-values”) as tsv: for line in csv.reader(tsv, dialect=”excel-tab”): #You can also use delimiter=”\t” rather than giving a dialect. … Where line is a list of the values on the current row for each iteration. Edit: As suggested below, if … Read more
You can easily enough write a function that calls the readFile action, and passes the result to your index function. readAndIndex fileName = do text <- readFile fileName return $ index text However, the IO monad taints everything that uses it, so this function has the type: readAndIndex :: FilePath -> IO [(String, [Integer])]
Situation in Python3 according to the docs: io.open(file, *[options]*) This is an alias for the builtin open() function. and While the builtin open() and the associated io module are the recommended approach for working with encoded text files, this module [i.e. codecs] provides additional utility functions and classes that allow the use of a wider … Read more
A quick Google revealed a thread on Coderanch which was useful. There are several other ways of doing console writing but there seems to be no real benefit of using one or the other apart from less code to write and that the creation of a new PrintWriter object will take up more memory (eventually). … Read more
There are a number of things wrong with your code: char *readFile(char *fileName) { FILE *file; char *code = malloc(1000 * sizeof(char)); file = fopen(fileName, “r”); do { *code++ = (char)fgetc(file); } while(*code != EOF); return code; } What if the file is greater than 1,000 bytes? You are increasing code each time you read … Read more