0

i kept my text file at exactly same place where .exe is existing , then also its not working .. hi this is my code , i kept my text file at exactly same place where .exe is existing , then also its not working .. hi this is my code , i kept my text file at exactly same place where .exe is existing , then also its not working ..

   int main(int argc, _TCHAR* argv[])
{
    int result = 0;




  char ca, file_name[25];
   FILE *fp;

   //printf("Enter the name of file you wish to see\n");
   gets(file_name);

   fp = fopen("sample.txt","r"); // read mode

   if( fp == NULL )
   {
      perror("Error while opening the file.\n");
      //exit(EXIT_FAILURE);
   }


   if( fgets (str, 60, fp)!=NULL ) 
   {
      /* writing content to stdout */
      puts(str);
   }

   fclose(fp);


}
  • str ? Define str first char str[80] , There is lots of problem in your code ! watch my code , may be this will help – udit043 Jun 26 '15 at 13:38

1 Answers1

0

Try this , i basically work in C & C++ , i use this code to perform file operation

int main()
{
  char filename[10];char extension[5]=".txt"; 
  printf("Enter the name of file you wish to see\n");
  gets(filename);
  fflush(stdin);
  filename[10]='\0';
  strcat(filename,extension);
  puts(filename);

  FILE *p; char acline[80];
  p=fopen(filename,"r");
  if(p==NULL)
  {
      printf("%s file is missing\n",filename);system("pause");
  }

  fseek(p,0,SEEK_SET);     // Setting file pointer to beginning of the file
  while (!feof(p))         // Detecting end of file
  {
     fgets(acline,80,p);  
     puts(acline);
  }
  printf("\n File end\n");
  system("pause");
}

*but while(!feof()) has certain issues see this

Community
  • 1
  • 1
udit043
  • 1,610
  • 3
  • 22
  • 40