Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How can I read in a line of text, one by one, and store in an array? (http://www.go4expert.com/forums/i-read-line-text-store-array-t3706/)

Gcam 1Apr2007 11:08

How can I read in a line of text, one by one, and store in an array?
 
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

DaWei 1Apr2007 19:25

Re: How can I read in a line of text, one by one, and store in an array?
 
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.

wrecker 3Apr2007 14:03

Re: How can I read in a line of text, one by one, and store in an array?
 
: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.... ;)

DaWei 3Apr2007 17:46

Re: How can I read in a line of text, one by one, and store in an array?
 
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.

wrecker 4Apr2007 01:02

Re: How can I read in a line of text, one by one, and store in an array?
 
OOps ya, made a mistake. I think i didn't read the question carefully.

vignesh1988i 6Oct2009 19:22

Re: How can I read in a line of text, one by one, and store in an array?
 
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....... :):):):)


All times are GMT +5.5. The time now is 11:59.