1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How can I read in a line of text, one by one, and store in an array?

Discussion in 'C' started by Gcam, Apr 1, 2007.

  1. Gcam

    Gcam New Member

    Joined:
    Apr 1, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I'm a major C noob but trying real hard to understand what's going on. I would simply like to read in, line by line, from a text file and have each line stored in an array. In other code, like VB, I would simply do something like:

    String myArrayOfLines(200);
    i=0;
    For each line in myTextFile
    myArrayOfLines(i) = aLinefromMyTextFile;
    i++;
    Next Line

    But I realize this isn't possible with quite the ease. I've spent days trying to get something to work with multidimensional arrays of char type and having no luck.

    I could really use some direction on how to store each line of text from a text file into an array, and then be able to print them all out, line by line.

    Thanks a bunch !

    Gcam
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Please read the "Before you make a query" thread before you post again. To post without doing so (or while ignoring its recommendations) is rude.

    C does not have a string type such as you show (C++ does). A C string IS an array. It's an array of char, terminated by a zero ('\0'). Consequently, an array of strings is a 2D array of char. For an actual 2D array (fully contiguous) one would need to make the 'rows' at least one char longer than the longest string to be read, or else some string(s) will be truncated. One can get around that by reading a line, getting storage of the correct size for the string from malloc (which returns a pointer), transferring the temp string to that area, and storing those pointers in an array.

    Use 'fgets()' to read the line into temporary storage. You should attempt to make this storage longer than the longest anticipated line. fgets allows you to specify the maximum length to be read, so you may truncate a line if you have guessed incorrectly, but you won't generate a buffer overflow.

    To print them out, merely use printf with a string format.

    You would find this much more familiar if you were using C++ and its string type. I perceive that you have not really studied C for spit. The basic "Hello World" program would have taught you how to print a line. I would suggest a book or tutorials. There are good tutorials referenced on the forum. Forums are better as help than as a primary learning entity.
     
  3. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    :rolleyes: Just try using getline() function. this function will read string untill it encounters a '\n' or termination count provided in function itself. This is quite simple. Better read a good C programming. If you get to read, must fo through K&R or Gottfried. If you a mewbie in C programming better read Yashwant kanetkar.Its ideal for new comers.... ;)
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Wrecker, that's not correct. C does not have a getline function. C++ does. The OP distinctly says, 'C'. Please be careful about misleading the questioners.
     
  5. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    OOps ya, made a mistake. I think i didn't read the question carefully.
     
  6. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    if am having a file , in that the contents is :
    iam a citizen of india
    my name is indian
    iam a perfect human being.
    I THINK , THIS U DON'T KNOW HOW TO STORE IN AN ARRAY ?? isn't!
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    FILE *pointer;
    char ch1[50];
    int i=0;
    pointer=fopen("srv.txt","r");
    while(1)
    {
       ch1[i]=fgetc(pointer);
           if(!(ch1[i]^EOF))
           break;
    i++;
    }
    ch1[i]='\0';
    for(i=0;ch1[i]!='\0';i++)
    printf("%c",ch[i]);
    getch();
    }
    hope this will work and you could easily understand....... :):):):)
     
    Last edited by a moderator: Oct 6, 2009

Share This Page