0

I am new to C and I am confused how I can read through a file and store each line to an index of an array.

Example file:

What color is the sky?
Red
Orange
Yellow
Blue

Desired result of code:

input[0] = What color is the sky?
input[1] = Red
input[2] = Orange
input[3] = Yellow
input[4] = Blue

Hhere is what I have so far:

char input[60];

//declare string array of size 80, for 80 lines

for(int i = 0; fgets(input, sizeof(input), inFile)!=NULL; i++){ 

    //string[i] = input; storing this line to the string index

}

//later on use the string[80] that now has all lines

I understand that declaring input[60] is only determining the length of each line, not the number of lines. I am so used to thinking about strings in other coding languages, that the use of char is throwing me off. I have tried video tutorials but they didn't help me.

12
  • Make string an array of arrays of characters, and use strcpy? Commented Sep 19, 2018 at 5:32
  • Are you familiar with dynamic memory allocation via malloc() et al? If so, you could use that to make copies of the strings (lines) you read. If you've not covered malloc() and free() yet, then use the simpler but less flexible 2D array of characters. Don't forget to remove the newline that fgets() includes in the data. (Since you've got a comment about a string array of size 80 for 80 lines, what do you plan to write there? Why didn't you write it?) Commented Sep 19, 2018 at 5:45
  • 1
    If you aren't sure how to declare a 2D array in C, it is difficult to help you without regurgitating what your text book should be telling you. We will help honest efforts to solve a problem, but we don't simply write the code for you. There are a number of ways that your problem could be tackled; we don't know which your course notes and lectures expect you to use. Commented Sep 19, 2018 at 5:59
  • 1
    Yes, using char array[80][70]; would allow you to store up to 80 lines of up to 70 characters each, where the character count includes the terminating null byte (so up to 69 displayable characters and one null byte at the end). It's not obvious why strcpy(array[i], input) would crash your program unless you get more than 80 input lines. You should check that you don't go beyond the end of your array (that i does not reach or exceed 80). Please read about creating an MCVE (minimal reproducible example). That helps us to help you better. Commented Sep 19, 2018 at 6:15
  • 1
    Thank you JonathanLeffler and @Some Programmer dude! I solved it with your guidance. Commented Sep 19, 2018 at 6:48

1 Answer 1

-1

Each line of the file is a different string, and each string is a char* pointer to an array; so what you are needing is a 1D array of char* pointers (or alternately a 2D array of char.)

char *line[ MAX_LINES ];  // 1D array of char* pointers.

You can either initialize a two dimensional array of char, or you can malloc the memory for each line of the 1D char* pointers.

Here's an example of the malloc approach. The variable it's stored in is called "line" rather than "input"; but you can exchange the variable names if you like, and change the print formatting to solve your particular problem. This is just intended as an example of reading the strings into memory, in the same way you desire. No practical system has less than 4K of heap space, so I've omitted the malloc memory check.

#include <string.h>                                                             
#include <malloc.h>                                                             
#include <stdio.h>                                                              

#define MAX_LINES 10                                                            
#define MAX_LEN   60                                                            

int main( int narg, char *arg[] ) {                                             
    char input[ MAX_LEN ];                                                      
    char *line[ MAX_LINES ];                                                    
    FILE *inFile;                                                               
    int i,j;                                                                    

    if ( narg != 2 ){fprintf( stderr, "Use: %s filename\n", arg[0] ); return 2;}  
    if (!(inFile=fopen(arg[1],"r") )){
          fprintf( stderr, "Can't open '%s'\n",arg[1]);
          return 2;
    }

    for ( i=0; i<MAX_LINES && fgets(input, sizeof(input), inFile ); ++i ) {     
        int lineLen=strlen(input) + 1;                                          
        line[i] = strncpy( malloc( lineLen ), input, lineLen );                 
    }                                                                           

    for ( j=0; j<i; ++j ) { printf( "Line %d:%s", j+1, line[j] ); free(line[j]); }
    fclose(inFile);                                                             
    return 0;                                                                   
}        
Sign up to request clarification or add additional context in comments.

4 Comments

The style indentation presented here is not OK, and would get you a "please explain" in any code review and/or programming assignment.
Please don't put multiple statements on a single line as you show; white space (newlines) are cheap, even on Stack Overflow. I observe that you include the newlines in the saved data. That's permissible and allows you to omit the newline at the end of the format string in the printf() statement You should check that malloc() succeeds before using it in line[i] = strncpy( malloc( lineLen ), input, lineLen );. While that may work most of the time in test programs, it is not safe for professional development work.
@kingsley; Yes, it would. But any C programmer KNOWS the meaning of curly braces, and short lines of 80 characters or less can be seen at a glance. The formatting is on purpose, for if the opening poster is copying this for homework, without understanding, they will get a penalty. If they are trying to "learn", about a specific detail ... they will write their own code for the homework assignment.
strlen, strncpy, malloc and copy could be replaced by strdup...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.