read file into array

This is a discussion on read file into array within the C Programming forums, part of the General Programming Boards category; I have a text file contains the following: apple meaning of apple degree meaning of degree How would I read ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    18

    read file into array

    I have a text file contains the following:
    apple meaning of apple
    degree meaning of degree
    How would I read each line and put them into array of char?
    I have the folloing but need some advice on how to do it, please.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define MAXLINE 10
    void main(void)
    {
    	char* readline[MAXLINE];
    	FILE *infile;
    	char* line;
    	int ch;
    
    	if((infile = fopen("info.txt", "r"))==NULL )
    	{
    		fprintf(stderr, "Error: Cannot open input file. ");
    		exit(1);
    	}
    
    	line = (char *)malloc(sizeof(char));
    
    	while(fscanf(infile, "%s", line) != EOF)
    	{
    		printf("%s\n", line);
    
    		while (ch = fgetc(infile) != '\n')
    		{
    			advice.....
    		}
    
    	}
    }
    Last edited by unhwan; 06-26-2002 at 06:30 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How would I read each line and put them into array of char?
    I have the folloing but need some advice on how to do it, please.
    fgets is your friend.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    void main... in case you didn't see this in my reply to your other post

    >line = (char *)malloc(sizeof(char));
    you don't need the cast of the malloc return, loose it!
    >line = malloc(sizeof(char));
    Hey wait a min..... your malloc'ing one byte of memory, you aren't going to store much in there!
    If you wanting a temp buffer to store data in as you read it, use something like
    >char line[100]; /* or whatever length */

    And yes, as Quzah said, drop fscanf() and fgetc(), and use fgets(). Once you have the complete line into one array, you can then think about splitting the line up by words if so you wish.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main(void)
    No, int main ( void )

    >fprintf(stderr, "Error: Cannot open input file. ");
    perror would work better here.

    >exit(1);
    An exit condition of 1 is non-standard, use either 0, EXIT_SUCCESS, or EXIT_FAILURE. The latter two are in stdlib.h.

    >line = (char *)malloc(sizeof(char));
    In C you don't need the cast and often it can hurt more than help. You are also only trying to allocate one byte, this is probably not what you want.

    >while(fscanf(infile, "%s", line) != EOF)
    This gives you one word at a time, not one line. For a complete line, fgets would be the better function to use.

    My recommendation is to declare line as a static array with a size large enough to hold the length of the lines. Each time you read a new line, allocate space for that string in the next empty space of readline. Copy the string from line to readline with strcpy, increment a counter for readline and do it again:
    Code:
    while(fgets(line, sizeof line, infile) != NULL && i < MAXLINE)
    {
      printf("%s\n", line);
      readline[i] = malloc(strlen(line)+1);
      if(readline[i] != NULL)
        strcpy(readline[i++], line);
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    18

    how would i?

    I have array of string which have the following:
    one meaning of one
    two meaning of two
    three meaning of three

    For example, "one" is a word and "meaning of one" is a meaning of one. How would I divide each line into two pieces so I can have word and meaning separately?

    I would like to use getline but don't know how to use it.
    I want to use a first space to separate those into word and meaning.
    Please let me know. thanks

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    18
    This is what I have and it works... now how do i divide the each line into two separate pieces...
    for example, char *array[i] = "apple meaning of apple"
    I want to divide "apple" and "meaning of apple" separately.
    Can I use getline? How do i use it?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MAXCHAR 30
    #define MAXLINE 10
    
    int main(void)
    {
    	char *line[MAXLINE];
    	char templine[MAXCHAR];
    	FILE *infile;
    	int i=0, count=0, len, ch;
    
    	if((infile = fopen("info.txt", "r"))==NULL )
    	{
    		perror("Cannot open input file");
    		return EXIT_FAILURE;
      
    	}
    
    	while(fgets(templine, sizeof templine, infile) != NULL)
    	{
    
    		line[i] = malloc(strlen(templine)+1);
    		len = strlen(line[i]);
    	
    		if (line[i] != NULL)
    			strcpy(line[i++], templine);
    
    		count++;
    	}
    	
    
    	for (i=0; i<count; i++)
    		printf("line %d has: %s", i, line[i]);
    	printf("\n");
    
    	fclose(infile);
    
    	return EXIT_SUCCESS;
    }
    Last edited by unhwan; 06-27-2002 at 01:46 AM.

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I have array of string which have the following:
    one meaning of one
    two meaning of two
    three meaning of three

    For example, "one" is a word and "meaning of one" is a meaning of one. How would I divide each line into two pieces so I can have word and meaning separately?

    I would like to use getline but don't know how to use it.
    I want to use a first space to separate those into word and meaning.
    Please let me know. thanks
    I've answered this same question twice already, so I'm not going to do it again. If you want my answers, check back in the other threads you posted this.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Read from file to an array
    By brooklyn1126 in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 09:32 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21