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 have a 2d array of characters and now i am trying to read from it and writing to a file. The thing is that the file seems to get created but there is nothing in it. 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
    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
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    It's a good idea to close files after you are finished with them.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading bitmap into array
    By kallistine in forum C Programming
    Replies: 35
    Last Post: 07-26-2009, 03:44 PM
  2. 2D array of threads?
    By shorinhio in forum C Programming
    Replies: 2
    Last Post: 01-04-2007, 04:02 PM
  3. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM