Thread: Haaaaalp

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    22

    Haaaaalp

    Code:
    #include<stdio.h>
    #include"global.h"
    #include<string.h>
    
    
    /*print_array()*/
    void print_array(int ia[][NUM_COLUMN], int row, int column);
    read_array(FILE *fp, FILE *fp2, char string1[], char string2[]);
    	int arraya[NUM_ROW][NUM_COLUMN];
    	int arrayb[NUM_ROW][NUM_COLUMN];
    	int arrayc[NUM_ROW][NUM_COLUMN];
    
    int main(int argc, char **argv){
    	char txt[]="txt";
    	FILE *fp;
    	FILE *fp2;
    
    	if(argc!=3){
    		printf("Usage: %s matrix_(letter).txt\n", argv[1]);
    		return -1;
    	}
    	if((strstr(argv[1],txt)!=0) && (strstr(argv[2],txt)!=0)){
    		printf("1st argument = %s \n", argv[1]);
    		printf("2nd argument = %s \n", argv[2]);
    		fp=fopen("matrix_a.txt", "r+");
    		fp2=fopen("matrix_b.txt", "r+");
    		printf("fp =%d\n", fp);
    		printf("fp2 =%d\n", fp2);
    		if(fp && fp2){
    			read_array(fp, fp2, argv[1], argv[2]);
    			fclose(argv[1]);
    			fclose(argv[2]);			
    		}else{
    			printf("%s and %s Not Open\n", argv[1], argv[2]);
    			exit(-1);
    		}		
    	}
    	else
    		printf("Unrecognized Arguments");
    
    	return 0;
    }/* main()*/
    
    /*print_array()*/
    /* ia is 2-Dimensional Array, 
    	Row is # of rows, 
    	Column is # of columns */
    read_array(FILE *fp, FILE *fp2, char string1[], char string2[]){
    		
    }
    
    
    void print_array(int ia[][NUM_COLUMN], int row, int column){
    	int i,j;
    	for(i=0;i<row;i++){
    		for(j=0;j<column;j++)
    			printf("%d ", ia[i][j]);
    		printf("\n");
    	}
    }

    why doesn't the file open? It is there i guarantee it. Please help me

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why are you opening matrix_a and matrix_b regardless of what gets typed in? And are they in the right directories?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    That was just to check, and they are in the right directories, i have copied them everywhere..

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do they both not open?

    And you can't fclose a char*, you can only fclose a FILE*.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    yeah neither of them, and fixed the fclose lol whoops. maybe it's just a random occurence can you try it in your compiler w/ fake .txt files. I will give you 5 dollars!!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    One thing the compiler noticed that I didn't is that your function and your prototype for read_array need a return type (presumably void) -- right now, read_array is just this thing, that might be a global variable, or something, but who knows.

    Once I fixed that, it works fine.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    thats all you did? just put type void? hmm....... thanks for you trouble pal but maybe its my compiler...

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If your compiler can't handle fopen, you really need another compiler. Granted, I am not aware of a compiler that can't open a file. I would really really check that you have a "random_a.txt" and not, for instance, "randoma.txt" or "random_a.dat", or "random_a.mat", or "random_a.text" and that that file is in the same directory as your executable file (wherever that may be). (Or, heaven help us if you tried to create a .txt file in Visual Studio, "random_a.txt.cpp".

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can't just say "maybe it's my compiler" without actually telling us which one.

    Let's consider some of the possibilities.
    1. Miracle C - yeah right, this is a broken toy. If you got this far without problems, I'm amazed.

    2. Turbo C, or other DOS relic from the past.
    Perhaps you're using XP as your host OS, maybe the file has spaces somewhere in the filename or directory path (c:\My Documents\anyone)

    3. r+ means reading and writing. Did you set permissions to read-only?

    Try
    Code:
    fp=fopen("matrix_a.txt", "r+");
    if ( fp == NULL ) {
      perror("Can't open matrix_a.txt" );
      exit( 1 );
    }
    The additional text should describe WHY the file could not be opened.
    Guesses include
    - not found
    - no permission
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    I seem to be getting a runtime error for this code. It pretty much is the same code that bobdole11 posted, although he had a cboard account and I did not at the time. I got the prior working just fine but it seems now I am getting a runtime error either with just inputting from the file in read_array or simply just with how I pass by reference with the 2d Array
    Also, I thought about using fgets, but not sure if I want to go through and parse everything.

    Any help would be appreciated. Thanks in advance.



    NUM_ROW and NUM_COLUMN defined in header file global.h

    Also example txt file would be: first two numbers are dimensions of the matrix, the rest is the matrix
    3 3
    1 2 3
    4 5 6
    7 8 9
    Last edited by Logicbynumbers; 11-17-2008 at 06:05 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If the data in the matrix file is larger than NUM_ROW by NUM_COLUMN you're going to trash some memory. void print_array needs to take some arguments. This line
    Code:
    if((fp!=0)&&(fp!=0)){
    needs a 2 in it. But what kind of crash are you getting, and how do you know?

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    NUM_ROW and NUM_COLUMN are 500 each. and the matrices I will be doing are 100 x 150. I haven't implemented print_array yet, it is commented out. I am not getting an error with the code you quoted. when I compile the code it stops working, the program. Vista says it stops working etc.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Oh right:
    Code:
    	for(i=0;i<row1;i++){
    		for(j=0;j<column1;j++)		
    			fscanf(fp,"%d", &array1[i][j]);	
    		fscanf(fp,"\n");
    	}
    
    	for(i=0;i<row2;i++){
    		for(j=0;j<column2;j++)
    			fscanf(fp2,"%d", &array2[i][j]);				
    		fscanf(fp2,"\n");
    	}

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    That would help, thanks. Also, I am having trouble with passing by reference for the array. Shouldn't it work the way I have it? I have tried multiple ways and it seems that I either have missing subscripts and

    different array subscripts : 'int (*)[1]' and 'int [500][500]

    Code:
    void func(int a[][], int b[][]);
    
    int main(){
    
    int ia[NUM_ROW][NUM_COL];
    int ib[NUM_ROW][NUM_COL];
    func(ia, ib);
    } void func(int a[][], int b[][]){ //code }

    Isn't this valid? Yet I get compile errors.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No. The second dimension in a function prototype MUST be given. Also, to pass an array by reference to a function, you use the array name by itself -- no &, no []. An array name, used by itself, decays to the right thing that you want to pass to a function (a pointer to its first element).

Popular pages Recent additions subscribe to a feed