File not opening issue in VC++ 2008 compiler

This is a discussion on File not opening issue in VC++ 2008 compiler within the C Programming forums, part of the General Programming Boards category; Hi, I am not able to open the file in read mode.Can u please tell me where i went wrong?.. ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    5

    File not opening issue in VC++ 2008 compiler

    Hi,

    I am not able to open the file in read mode.Can u please tell me where i went wrong?..

    I guess the value in buffer variable is not working .IF it so please kindly suggest me any solution.


    Code:
    int main(int argc, char* argv[])
    {
      int k=0;
      FILE *fp;
      unsigned char cd[255];
      unsigned long cd1[500];
      char buffer[1024];
      unsigned long sa=80044;
      int j=0,i=0,n=0;
      DIR *dir;
      struct dirent *direntry; //could be a file, or a directory
    
      dir = opendir("C:/Documents and Settings/Administrator/Desktop/vicky");
      if(!dir) {
      printf("Error: directory did not open!\n");
      return 1;
      }
    
      while((direntry=readdir(dir))!=NULL) {
      if(++k < 100)
      {
         printf("%s\n",direntry->d_name);
         sprintf(buffer,"%s",direntry->d_name);
         fp=fopen("buffer","rb");
         sa=sa-44;
         sa=sa/8;
    
      if(fp==NULL)
     {
       printf("file not found!");
     }
     else
          {
           for(j=0;j<(sa);j++)
           {
               for(i=0;i<8;i++)
               {
                   cd[i]=fgetc(fp);//get each character from file 
    
                  // printf("%c",cd[i]);
    
               }
               if(i==8)//if the i is 8 the character then do the following,just read 8 bytes  and then calculate the cd1.
               {
                    sa=sa-8;
                   cd1[j]=(cd[6] * 65536 + cd[5] * 256 + cd[4]);//multiply  the positional weightage and calculate the cd1,which contains the 7  digits decimal value.
    
                   //if((cd1[j]> 0x7FFFFF)&&(cd1[j]<=0xFFFFFF))
    
                     //cd1[j]=cd1[j]- 0xFFFFFF;
                     //cd1[j]=cd1[i] * -1;
    
               }
             printf("completes first 8 bytes read:%d - %d",j,cd1[j]);//print j and cd1[j] value in console window
    
           }
          fclose(fp);//close the file
       }
    
    
      }
      if((strcmp(direntry->d_name, "text.txt"))==0) {
         printf("\nThe %s file has been found\n",direntry->d_name);
    
         k=-99;  //just a flag value to show the file was found
         break;
       }
    
      }
      if(k!=-99)
      printf("\nThe test.txt file was not found\n");
    
      closedir(dir);
    
      printf("\n");
      getchar();
      return 0;
    
     }
    Last edited by keerth516; 01-17-2012 at 04:17 AM. Reason: changed the code

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Code:
    fp=fopen("buffer","rb");
    You tried to open a file named "buffer", which is not the variable buffer. Also if the variable buffer doesn't point to a file that exists, fopen will continue to fail.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    Code:
    fp=fopen("buffer","rb");
    You tried to open a file named "buffer", which is not the variable buffer. Also if the variable buffer doesn't point to a file that exists, fopen will continue to fail.
    I have changed to fp=(buffer,"rb");
    Still i get the file not found error.which i made it as error statement if the file cannot read ....how to correct this line to read the file (buffer variable value contains the filename.b11)..binary fiel

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    I already tried to explain that if you didn't point to a file that existed, it wouldn't open in my previous post.

    You opened this directory:

    "C:/Documents and Settings/Administrator/Desktop/vicky"

    Probably to do some file tree walking. Anyway, the point is that the computer would want a file like "filename.b11" to be in the current working directory, because you are using relative pathing. In some ways that is good. Your desktop is not the current working directory. The easiest thing to do is to use what's called absolute pathing, and stick the directory you opened in front of the file name. (Absolute pathing is so named because it starts from the root, a drive letter, and works its way down to your file.)

    You can also use the IDE to change the current working directory while you test but you will probably be forced into something more permanent if this is not a toy. Make sure you use the DIR* information and API to build a reasonable file path to the file so fopen will work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Integrate C++ compiler in Visual Studio 2008
    By kavinga86 in forum C# Programming
    Replies: 7
    Last Post: 06-16-2010, 11:06 PM
  2. Replies: 4
    Last Post: 09-12-2009, 01:10 PM
  3. Compiler Issue
    By Jesse20ghet in forum C++ Programming
    Replies: 35
    Last Post: 08-19-2009, 10:47 AM
  4. file opening issue
    By _lazycat_ in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2007, 04:20 AM

Tags for this Thread


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