Results 1 to 8 of 8

Thread: FILEs in C

  1. #1

    Angry FILEs in C

    what are the ways a null pointer is returned when i try to open a file inthe following line

    FILE *Fptr;
    Fptr=fopen("try.txt","r");

    I'm using turbo C++ 3 compiler (its an old box)
    and its on a windows system
    its unable to the file though its in the same folder etc and also this only happens some times not always it does manage to open the file some times why does this happen

    I am using the Cpp compiler for C

    thanx in advance
    anything that doesn\'t kill you or your dreams only makes you stronger

  2. #2
    perhaps this tutorial might make things more clear for you.

  3. #3

    Unhappy

    nope but thanx for trying i already put in the piece of code that tells it to exit (with a message that the file is not opening )
    what i want is the reason it couldnt be opening sorry if i wasnt clear the first time around
    some one please help
    bye and thanx again
    anything that doesn\'t kill you or your dreams only makes you stronger

  4. #4
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        FILE *file;
        
        file=fopen("test.txt","r");
        if(file==NULL)
        {
            printf("Error: opening file.");
            return EXIT_FAILURE;
        }
        printf("File opened successfully, now closing it again");
        fclose(file);
        return EXIT_SUCCESS;
    
    }
    works like a charm (if test.txt is in the same directory).
    try it with this code, if this doesn't work, then we'll have to think of another way...

  5. #5
    there goes my pride in my communication skills
    OK
    I'll try this again

    why dont files open
    in other words what reasons can exist for the file not opening

    I dont kow of any oter way to put this Q

    PS: whit scorpion I PM'ed you I hope that was OK
    anything that doesn\'t kill you or your dreams only makes you stronger

  6. #6
    Senior Member
    Join Date
    Jul 2001
    Posts
    420
    Hi ark_templar,

    Have you tried the perror statement? Replace printf("Error: opening file."); with perror(); or perror("Could not open file"):

    Cheers,
    -D
    If you spend more on coffee than on IT security, you will be hacked. What\'s more, you deserve to be hacked.
    -- former White House cybersecurity adviser Richard Clarke

  7. #7
    Junior Member
    Join Date
    Oct 2004
    Posts
    8

    Lightbulb

    hey i think i got ur q....
    accordin to me there may b several reasons for the file pointer to be null..
    like improper file name , improper flag for opening the file as "r" or "w"
    if the file to b opened is not havin proper permissions..

    also i found a link to customize the number of files available to the program which may be
    useful:
    http://vmlinux.org/~jakov/community....com/15527.html

    but i dont know exactly what is the case with ur error......
    Knowledge needs to be acquired

  8. #8
    well here is the code of which thelines were a part of its incomplete its meant to be a zipping program (yes only for text and yes its not good but its mine)
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    #include<math.h>
    int main()
    	{
    	int i=0,j,code=-1,x,no_bits,size_left,overflow;
    	char ascii[256][2],buffer_read[8],*buffer_write,letter=0;
    	char temp;//f_2_b_red[]="try.txt",f_2_b_riten[]="op.txt";
    	FILE *fptr1,*fptr2,*fptr3;
    	fptr3=fopen("demoth.txt","w");
    	for(i=0;i<256;i++)
    		{
    		ascii[i][0]=-1;
    		ascii[i][1]=0;
    		}
    	fflush(stdin);
          //	printf("enter the name of the text file you want to zip\n");
        //	scanf("%s",f_2_b_red);
        //	fflush(stdin);
       //	strcat(f_2_b_red,".txt");
         //	printf("enter the name of the text file you want to put it in\n");
           //	scanf("%s",f_2_b_riten);
      //	fflush(stdin);
        //	strcat(f_2_b_riten,".txt");
    	fptr1=fopen("try.txt","r");
    	if(fptr1==NULL)
    		{
    		printf("the file coouldnt be opened");
    		goto fin;
    		}
    	while(!feof(fptr1))
    		{
    		letter=fgetc(fptr1);
    		printf("%d\n",letter);
    		ascii[letter][1]++;
    		if(ascii[letter][0]==-1)
    			{
    			code++;
    			ascii[letter][0]=code;
    			fprintf(fptr3,"%c %d\n",letter,code);
    			}
    		}
    	printf("the final code is %d",code);
    	printf("the code after reduction is %d",code);
    	printf("\n");
    	//********************************
    	for(no_bits=0;no_bits<8;no_bits++)
    		if((code/(1<<no_bits))==0)
    			break;
    	//********************************
    	fprintf(fptr3,"%d the number of bits\n",no_bits);
    	fclose(fptr1);
    	buffer_write=(char*)malloc(no_bits*sizeof(char));
    	fptr2=fopen("op.txt","w");
    	fptr1=fopen("try.txt","r");
    	i=0;
    	while(!feof(fptr1))
    		{
    		buffer_read[i]=fgetc(fptr1);
    		printf("%c\t",buffer_read[i]);
    		printf("%d\n",i);
    		i++;
    		if(i==8)
    			{
    			printf("8 read\n now it should print the chars");
    			x=0;
    			size_left=8;
    			overflow=0;
    			for(i=0;i<8;i++)
    				{
    				fprintf(fptr3,"the value to be printed is %c its binary is %d\n",ascii[buffer_read[i]][0],ascii[buffer_read[i]][0]);
    				letter=ascii[buffer_read[i]][0];
    				fprintf(fptr3,"the char is initialy %d\n",letter);
    				if((size_left-no_bits)>0)
    					{
    					letter=letter<<(size_left-no_bits);
    					fprintf(fptr3,"the binary after shifting is %d\n",letter);
    					buffer_write[x]=buffer_write[x]|letter;
    					fprintf(fptr3,"the resultant char is %d\n",buffer_write[x]);
    					size_left-=no_bits;
    					}
    				else
    					{
    					size_left-=no_bits;
    					overflow=-1*size_left;
    					letter=letter>>overflow;
    					fprintf(fptr3,"the binary after shifting is %d\n",letter);
    					buffer_write[x]=buffer_write[x]|letter;
    					fprintf(fptr3,"the resultant char is %d\n",buffer_write[x]);
    					if(size_left<0)
    						{
    						size_left=8;
    						size_left-=overflow;
    						letter=letter<<size_left;
    						fprintf(fptr3,"the binary after shifting is %d\n",letter);
    						buffer_write[x]=buffer_write[x]|letter;
    						fprintf(fptr3,"the resultant char is %d\n",buffer_write[x]);
    						}
    					if(x<(no_bits-1))
    						x++;
    					}
    				}
    			for(j=0;j<no_bits;j++)
    				{
    				printf("%c",buffer_write[j]);
    				fputc(buffer_write[j],fptr2);
    				}
    			for(j=0;j<no_bits;j++)
    				buffer_write[j]=0;
    			printf("the char have been printed\n");
    			i=0;
    			}
    		}
    
    	fclose(fptr3);
    	fclose(fptr1);
    	fclose(fptr2);
    fin:	return(0);
    	}
    anything that doesn\'t kill you or your dreams only makes you stronger

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •