FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


C++ code help





Xcelerate
What's wrong with this code? When I run it, it sometimes works, and other times it gives me an error and does completely process the file.

Thanks.

Code:
#include <stdio.h>
#include <string.h>

int main(int argument_count, char *arguments[])
   {
   if (argument_count != 2)
      {
      printf("Usage: bytestream filename");
      return 0;
      }

   FILE *file_pointer;
   if ((file_pointer = fopen(arguments[1], "r")) == NULL)
      {
      printf("Error opening file %s", arguments[1]);
      return 0;
      }

   FILE *edit_pointer;
   char *edit_file = strcat(arguments[1], ".txt");
   if ((edit_pointer = fopen(edit_file, "w")) == NULL)
      {
      printf("Error opening file %s.txt", arguments[1]);
      return 0;
      }

   int byte;

   while ((byte = fgetc(file_pointer)) != EOF)
      {
      fprintf(edit_pointer, "%d ", byte);
      }

   if (fclose(file_pointer) == -1)
      {
      printf("Error closing file %s", arguments[1]);
      return 0;
      }

   if (fclose(edit_pointer) == -1)
      {
      printf("Error closing file %s.txt", arguments[1]);
      return 0;
      }

   printf("\n\nCompleted successfully!\n\n");

   return 0;
   }
shamil
Code:
#include <stdio.h>
#include <string.h>


  int main(int argument_count, char *arguments[])
   {

    printf("%d",argument_count);
   if (argument_count != 3)
      {
      printf("Usage: bytestream filename");
      return 0;
      }

   FILE *file_pointer;
   if ((file_pointer = fopen(arguments[1], "r")) == NULL)
      {
      printf("Error opening filtte %s", arguments[1]);
      return 0;
      }

   FILE *edit_pointer;
  // char *edit_file = strcat(arguments[1], ".txt");
   if ((edit_pointer = fopen(arguments[2], "w")) == NULL)
      {
      printf("Error opening file %s.txt", arguments[1]);
      return 0;
      }

   int byte;

   while ((byte = fgetc(file_pointer)) != EOF)
      {
      fprintf(edit_pointer, "%d ", byte);
      }

   if (fclose(file_pointer) == -1)
      {
      printf("Error closing file %s", arguments[1]);
      return 0;
      }

   if (fclose(edit_pointer) == -1)
      {
      printf("Error closing file %s.txt", arguments[1]);
      return 0;
      }

   printf("\n\nCompleted successfully!\n\n");

   return 0;
   }


Eneter filenames with extensions.
If you don't want to use extensions you can change code.
Related topics
C++ TUTORIAL : INTRODUCTION TO C++
What's wrong with this C code???
C++
VBulletin Portal Installing
This C code is little unclear to me.
C++ assignment help
Code::Blocks C++ IDE Help !
what function gives process id in C#
C++ code problem
10 frih$ contest: what does this c++ code do?
Slight problem with C code
Trojan-Clicker HTML IFrame.gv" removal from code help.
Basic C Programming Help?
C++ with the command line?, C++ for vista
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.