programming problem....C programming..not sure whether this can be done or not
Hey all,
i am in the middle of having trouble now....last time i did program php and i realized that there is some function that really useful for reading the content of text file....for example file_get_contents(...). so using this idea i want to create a c program that read the text file line by line.I noticed that there are some functions can do that for example fgets(...),fscanf(..).but when i tried them they dont give me something that i wish to do..what i want to do is that my c program read the text file line by line and give me the array output of string. For example mytext.txt have something like below: A:Berlin B:Munich C:Sydney D:London so i would have ArrayString[0] is equal to A:Berlin. can i do this with c programming..please help me if u guys have any ideas |
If you use GNU/linux, start with function getline: it allocates a buffer bug enough to store the loaded line.
|
Quote:
|
thanks for the information...will take a look at it today...will be back with questions later...
|
hey i guys i have tried to used getline and i can say i quite understand the document and i learnt it by hard.but when i tried it still doesn't give me the result that i want...for example this is my code, i got it from an example
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *fp; char *line= NULL; size_t len = 0; ssize_t read; fp = fopen("mytext.txt", "r"); if (fp == NULL) exit(EXIT_FAILURE); while ((read = getline(&line, &len, fp)) != -1) { // printf("Retrieved line of length %zu :\n", read); //printf("%s", line); printf("%c",line[0]); } free(line); return 0; } And my text.file looks like this: A:Munich B:Berlin C:London D:New York unfortunately when i compiled the c codes above, the output is like this A B C D what i want is to have the entire line for example line[0] would give me A:Munich line[1] would give me B:Berlin till line[3] D:New York can i do that?i keep on trying from last night and i couldn't get it..please help me |
Code:
while (line= NULL, (read = getline(&line, &len, fp)) != -1) { |
Quote:
If you want array of strings, it's like : char lines[10][128]; (declare an array of 10 strings with max 127 chars + 1 (for the \0 terminating char) then strncpy line to lines[x] in getline while loop |
Quote:
|
A string is an array by itself (an array of chars), so a list of strings is an array of arrays
if I use strncpy(lines[0], line, len), then line string is copied into lines[0] |
Code:
/* getlinetest.c */ |
Here is my example, linearray.h:
Code:
#ifndef LINEARRAY_H It is pretty efficient, although not optimal. It does one extra copy of the data, which can be avoided using much more complex code for the newline detection, by supporting single-byte newlines, or by trimming newlines. It should be faster than anything using fgets() or getline(). It reads the input file in 64k chunks, reallocating the buffer each time. This is not too efficient, but works for all input types (like pipes and sockets). For files, the reallocations can be minimized by first checking the file size. It is easy to modify the code so that newlines are stripped out (which also eliminates the extra copy). Leading and trailing whitespace and empty lines can be removed as well, with almost zero extra computing cost, by expanding the newline counting loop to trim whitespace around newlines. The downside is that extending a line returned by this function is a bit complicated. You basically have to allocate a new line, and copy the old line into it. Because the lines are all allocated in one big chunk, you cannot just reallocate one line. Here is an example program using the above code. It outputs each line indented by one tab. Code:
#include <stdio.h> Code:
struct command { |
thanks guys....u all really help me a lot...now my task is to understand what do u guys means from the code...anyways thanks a lot....really appreciate the codes and all your helps....thanks again
|
while ((read = getline (&line, &buflen, fp)) != -1) {
linelen= strlen (line); if (linelen>0 && line[linelen-1]=='\n') line[--linelen]= '\0'; if (nlines==nmaxlines) { nmaxlines= nmaxlines? 2*nmaxlines: 64; lines= realloc (lines, nmaxlines*sizeof(char *)); } lines [nlines++] = strdup (line); } free (line); hey i couldn't understand this code nmaxlines= nmaxlines? 2*nmaxlines: 64;..what it's means..is it means that if (nmaxlines=nmaxlines) take the values of 2*nmaxlines else take value 64...am i right? |
In other words:
Code:
if (nlines==nmaxlines) { |
thanks now i understand ur code well...thanks for the explanation..
|
All times are GMT -5. The time now is 06:31 PM. |