Creating a multi-dimension array with file input

This is a discussion on Creating a multi-dimension array with file input within the C Programming forums, part of the General Programming Boards category; I have to make this program for a computer programming class and we are given a file to input. The ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    13

    Creating a multi-dimension array with file input

    I have to make this program for a computer programming class and we are given a file to input. The file input looks like this:

    Nevada, IA
    February, 2000
    2 0.1
    4 0.4
    28 0
    29 0.01
    30 0.01
    9 0
    11 0.21
    13 0
    11 0
    15 0
    17 0.55
    19 1.8
    8 0.08
    20 1.12
    24 0

    The input file is the days of the month and some rain fail associated with the day of the month. I am trying to take the days of the month and make a multi dimension array with the data and i also want it to be an interger array. So far this is what I have and I am not sure where to go with it for that certain part. This is the main error that I get:
    "error C2664: 'fgets' : cannot convert parameter 1 from 'int [40][8]' to 'char *' "

    Of course I don't know what to do in the for loop yet but that's where i needed the help. Thanks!

    Code:
    #include "stdafx.h"
    
    int main()
    {
    	//arrays for the first two lines of input file
    	char location[25], month_year[25];
    	//array for each day of the month
    	int days[40][8], status = 0;
    
    	FILE *input, *output;
    	input = fopen("input.txt", "r");
    	output = fopen("output.xt", "a");
        
    	//fills in the arrays for the location and year
    	fgets(location, 25, input);
    	fgets(month_year, 25, input);
    
    	//fills in the array for the days of the month
    	for (status = fgets(days, 8, input);
    		 status != EOF;
    		 status = fgets(days, 8, input))
    	{
    		
    	}
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    what you want to do is put fgets into a buffer then parse that buffer for the info you need
    the faq has an entry about this
    use fgets
    then sscanf

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    How do I get rid of this error though??

    error C2664: 'fgets' : cannot convert parameter 1 from 'int [40][8]' to 'char *'

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    char buffer[BUFF_SIZE];
    fgets( buffer, sizeof(buffer), input);

    Have you read a FAQ?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    use fgets correctly, it wants a char * to store in
    example: char location[25], month_year[25];

    which you've used previously

    it has no idea how to store a line it reads from a file into a multidimensional array
    thats your job

    p.s. look into the dimensions of that array, I think you can pick some better numbers there

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    I got the fgets part to copy the first string into a buff array but i am not exactly sure how to use the sscanf function.

    This is what I have, but I need to get it to store it to the arrays days.
    This results in a runtime error.


    Code:
    int main()
    {
    	//arrays for the first two lines of input file
    	char location[25], month_year[25];
    	//array for each day of the month
    	int days[40][8], status = 0, n = 0; 
    	char buff[8];
    
    	FILE *input, *output;
    	input = fopen("input.txt", "r");
    	output = fopen("output.xt", "a");
        
    	//fills in the arrays for the location and year
    	fgets(location, 25, input);
    	fgets(month_year, 25, input);
    
    	//fills in the array for the days of the month
    	fgets(buff, 8, input);
    	sscanf(buff, "%d %d", days[n][8]);
    
    	return 0;
    }
    Last edited by zac_haryy; 12-06-2006 at 10:42 AM.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You are including <stdio.h>, right?

    Code:
    sscanf(buff, "%d %d", days[n][8]);
    You are passing only one too few parameters, and the last parameter should have an & before it. And the whole thing should perhaps be in a for loop.

    To write to the output file you can use fputs() or fprintf().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    Well I cant get it to go with a multi-dimension array and I think that it may be easier 2 seperate arrays (maybe...). Anyways here is what I am trying and I am still getting this error:
    Homework 7 (problem 1) error C2440: '=' : cannot convert from 'char *' to 'int'

    Code:
    int main()
    {
    	//arrays for the first two lines of input file
    	char location[25], month_year[25], temp[8];
    	//array for each day of the month
    	int n = 0, status, array_number = 0; 
    	float days[45], rain[45];
    
    	FILE *input, *output;
    	input = fopen("input.txt", "r");
    	output = fopen("output.xt", "a");
        
    	//fills in the arrays for the location and year
    	fgets(location, 25, input);
    	fgets(month_year, 25, input);
    
    	//fills in the array for the days of the month
    	for (status = fgets(temp, 8, input);
    		status != EOF;
    		status++)
    	{
    		int n = 0;
    		while (temp[n] != '\n')
    			n++;
    		sscanf(temp, "%f %f", &days[array_number], &rain[array_number]);
    		array_number++;
    	}
    
    	return 0;
    }
    And if I where to try the multi-dimensions (which may be easier to pass this to different functions) here is the code that I am trying for that part of it (I run into a runtime erroe when it gets to the sscanf part):
    Code:
    int main()
    {
    	//arrays for the first two lines of input file
    	char location[25], month_year[25];
    	//array for each day of the month
    	int status, n = 0, array_number = 0; 
    	char days[40][8], temp[8];
    
    	FILE *input, *output;
    	input = fopen("input.txt", "r");
    	output = fopen("output.xt", "a");
        
    	//fills in the arrays for the location and year
    	fgets(location, 25, input);
    	fgets(month_year, 25, input);
    
    	//fills in the array for the days of the month
    	for (status = 0;
    		 status != EOF;
    		 status++)
    	{
    		fgets(temp, 8, input);
    		int n = 0;
    		while (temp[n] != '\n')
    			n++;
    		sscanf(temp, "%f %f", &days[array_number][40]);
    		array_number++;
    
    	}
    
    	return 0;
    }
    Last edited by zac_haryy; 12-07-2006 at 12:07 AM.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    while (temp[n] != '\n')
    n++;
    You do nothing here
    read the FAQ how to remove '\n' character from the buffer

    sscanf(temp, "%f %f", &days[array_number][40]);
    you ask to read 2 tokens, but provide only one variable
    you ask to read 2 float values, but provide pointer to the char*

    Your first value is a day (int from the 0 to 31)
    Your second value is a double

    Maybe you should think about using a struct for storing this pair of values
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    I am still having trouble with this. I dont understand the errors that I am getting. I would like to try to make a for loop so that I can scan the file in until the end of the file, but I get errors when trying to build the solution. Here are the two errors that i receive when trying to build:

    error C2440: '=' : cannot convert from 'char *' to 'int'
    error C2440: '=' : cannot convert from 'char *' to 'int'


    I can't figure out how to get rid of these

    Also I am not familiar with "struct". I dont know what these are.



    Code:
    int main()
    {
    	//arrays for the first two lines of input file
    	char location[25], month_year[25];
    	//array for each day of the month
    	int status, n = 0, array_number = 0, days[45] = {0}; 
    	char buff[8];
    	double rain[45] = {0};
    
    	FILE *input, *output;
    	input = fopen("input.txt", "r");
    	output = fopen("output.xt", "a");
    
    	//fills in the arrays for the location and year
    	fgets(location, 25, input);
    	fgets(month_year, 25, input);
    
    	//fills in the array for the days of the month
    	for (status = fgets(buff, 8, input);
    		 status != EOF;
    		 status = fgets(buff, 8, input))
    	{
    		int n = 0;
    		while (buff[n] != NULL)
    			n++;
    		sscanf(buff, "%d %.2lf", &days[array_number], &rain[array_number]);
    		array_number++;
    	}
    
    	return 0;
    }
    Last edited by zac_haryy; 12-07-2006 at 09:05 AM.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    I could make a while loop until it would reach the end of the file but I still get this error:
    error C2446: '!=' : no conversion from 'int' to 'char *'
    error C2040: '!=' : 'char *' differs in levels of indirection from 'int'

    Code:
    	while (fgets(buff, 8, input) != EOF)
    	{
    		int n = 0;
    		while (buff[n] != NULL)
    			n++;
    		sscanf(buff, "%d %.2lf", &days[array_number], &rain[array_number]);
    		array_number++;
    	}

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    because fgets returns char* and EOF is int
    have you read the reference for the fgets?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    So would a while loop be eligable in this situation?
    Last edited by zac_haryy; 12-07-2006 at 09:32 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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