Variables in C-code

I'm new to c-programming in linux and would like help with a problem. From lines in a file i would like to store them in an variable. Ex.
File looks like:
(row1:) /usr/bin
(row2:) /usr/java/jdk1.3
(row3:) ...

Variable should look like:
line[1] = /usr/bin
line[2] = /usr/java/jdk1.3
line[...] = ...

The file might change it contents.

Hope anyone can help me with this.

Comments

  • : I'm new to c-programming in linux and would like help with a problem. From lines in a file i would like to store them in an variable. Ex.
    : File looks like:
    : (row1:) /usr/bin
    : (row2:) /usr/java/jdk1.3
    : (row3:) ...
    :
    : Variable should look like:
    : line[1] = /usr/bin
    : line[2] = /usr/java/jdk1.3
    : line[...] = ...
    :
    : The file might change it contents.
    :
    : Hope anyone can help me with this.
    :
    :

    [blue]Is this a C program instead of C++? It would be a lot easier in C++, but can be accomplished in C also. You just need to write your own linked list set of functions in C -- in C++ they are already available. If the number of lines, or number of strings, in the file are changable, then you will certainly need a linked list. If the number of strings does not change, then you can use a character array to hold the strings. For simplicity lets assume the number of string do NOT change. Rather than write the whole program for you, I will just describe what I think you should do.

    [code]
    #define MAX_STRINGS 32
    #define MAX_CHAR_PER_STRING 80

    char line[MAX_STRINGS][MAX_CHAR_PER_STRING];
    int main()
    {
    FILE* ptr;
    int LineCounter;
    /* The following is just sudo-code -- it don't compile
    INITIALIZE LINE COUNTER TO ZERO
    OPEN THE FILE FOR READING
    WHILE NOT END-OF-FILE
    READ ONE LINE
    PUT IT INTO THE BUFFER
    INCREMENET LINE COUNTER
    END OF WHILE LOOP
    CLOSE THE FILE
    */

    }

    [/code]

  • Try the code below..

    ar.

    main() {
    char *lines[1024];
    int ctr=0;
    FILE *fp;

    fp = fopen("./t.txt", "r");
    while ( !feof(fp) ) {
    lines[ctr] = (size_t *) malloc(1024);
    fgets(lines[ctr], 1024, fp);
    printf("line[%d] = %s", ctr+1, lines[ctr]);
    ctr++;
    }
    fclose(fp);
    }


    : I'm new to c-programming in linux and would like help with a problem. From lines in a file i would like to store them in an variable. Ex.
    : File looks like:
    : (row1:) /usr/bin
    : (row2:) /usr/java/jdk1.3
    : (row3:) ...
    :
    : Variable should look like:
    : line[1] = /usr/bin
    : line[2] = /usr/java/jdk1.3
    : line[...] = ...
    :
    : The file might change it contents.
    :
    : Hope anyone can help me with this.
    :
    :

  • Thanks for the help.
    I'm going to compare the variable with the argv[i++] and the problem is how to get the same length of textstring in the variable as in the file though there is some garbage using size 1024.

    : Try the code below..
    :
    : ar.
    :
    : main() {
    : char *lines[1024];
    : int ctr=0;
    : FILE *fp;
    :
    : fp = fopen("./t.txt", "r");
    : while ( !feof(fp) ) {
    : lines[ctr] = (size_t *) malloc(1024);
    : fgets(lines[ctr], 1024, fp);
    : printf("line[%d] = %s", ctr+1, lines[ctr]);
    : ctr++;
    : }
    : fclose(fp);
    : }
    :
    :
    : : I'm new to c-programming in linux and would like help with a problem. From lines in a file i would like to store them in an variable. Ex.
    : : File looks like:
    : : (row1:) /usr/bin
    : : (row2:) /usr/java/jdk1.3
    : : (row3:) ...
    : :
    : : Variable should look like:
    : : line[1] = /usr/bin
    : : line[2] = /usr/java/jdk1.3
    : : line[...] = ...
    : :
    : : The file might change it contents.
    : :
    : : Hope anyone can help me with this.
    : :
    : :
    :
    :



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories