Thread: reading from a 2d array

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    reading from a 2d array

    I want to read from a 2d array which has strings in it. The thing is that the file seems to get created but when i am trying to read from the array has nothing stored in the file. The file is empty even after the write function. Please let me know where i am going wrong.

    [insert]
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    #define MAX_WORD_LENGTH 31
    #define MAX_WORDS 50
    
    /* Function prototypes */
    int readFile(char* inputFileName, int* wordCount, char array[][MAX_WORD_LENGTH], int flag);
    //int qsort(char array[][MAX_WORD_LENGTH], int* wordCount);
    int writeToFile(char* outFileName, int wordCount, char array[][MAX_WORD_LENGTH]);
    //void printTime(time_t  begin);
    void usage(char* processName);
    // enter any other function prototype you may wish
    
    int main(int argc, char* argv[])
    {
    		char array[MAX_WORDS][MAX_WORD_LENGTH];
    
    		int wordCount = 0; 
    		
           	switch (argc)
    		{
    		case 3:
    			readFile(argv[1], &wordCount, array,0);
    			writeToFile(argv[2], wordCount, array);
    			break;
    		case 4:
    		{
    			if (strcmp(argv[1], "-s") == 0)
    			  {
    			   readFile(argv[2], &wordCount, array,1);
         		   writeToFile(argv[3], wordCount, array);
    			  }
    			else if (strcmp(argv[1], "-t") == 0)
    			  {
    			    readFile(argv[2], &wordCount, array, 2);
    			    writeToFile(argv[3], wordCount, array);
    			  }
    			else
    			  {
    			    usage(argv[0]);
    			  }
    			break;
    		}
    		case 5:
    		default:
    	        usage(argv[0]);
    	        break;
    
    		
    	}
    	return 0;
    }
    
    // Define all your functions here
    
    int readFile(char* inputFileName, int* wordCount, char array[][MAX_WORD_LENGTH], int flag)
    {
    	FILE *fp;
    	fp = fopen(inputFileName, "r");
    
    	if(fp == NULL)
    	{
    		printf("\n Unable to open the file. Aborting");
    		exit(EXIT_FAILURE);
    
    	}
    
    	while(fscanf(fp, "%s", array[(*wordCount)++]) > 0);
    
    		return 0;
    }
    
    int writeToFile(char* outFileName, int wordCount, char array[][MAX_WORD_LENGTH])
    {
    	int i = 0;
    	FILE *fp;
    	fp = fopen(outFileName, "w");
    
    	if(fp == NULL)
    	{
    		printf("\n Unable to open the file. Aborting");
    		exit(EXIT_FAILURE);
    	}
    	
    	for(i = 0; i< wordCount; i++)
    	{
    		fprintf(fp, "%s\n", array[i++]);
    	}
    
    	return 0;
    }
    
    void usage(char* processName)
    {
      printf("Usage %s [-s] [-t] inputFileName outputFileName\n", processName);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need to close files
    you increment i twice in your write loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by vart View Post
    you need to close files
    you increment i twice in your write loop
    Thanks i got it working. But even if i was incrementing 'i' twice shouldnt have some of the alternate strings been written or was it because of my not closing the file.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by roaan View Post
    Thanks i got it working. But even if i was incrementing 'i' twice shouldnt have some of the alternate strings been written or was it because of my not closing the file.
    due to buffering you may not see changes in the file till you close file. Probably you tried to check file without closing the program...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Also can i use the qsort function defined in c to sort the elements of a 2d array which are strings.

    qsort - C++ Reference

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by roaan View Post
    Also can i use the qsort function defined in c to sort the elements of a 2d array which are strings.

    qsort - C++ Reference
    if this is a question - the answer is yes
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from a 2d array
    By roaan in forum C Programming
    Replies: 2
    Last Post: 09-04-2009, 06:30 AM
  2. reading bitmap into array
    By kallistine in forum C Programming
    Replies: 35
    Last Post: 07-26-2009, 03:44 PM
  3. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 12:34 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM