Turbo C++ question

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Turbo C++ question

Post by Neo »

This program gives me an error when i compile and execute it using Turbo C++ under Windows/DOS (the one with the fancy IDE). Using GCC (DJGPP) however to compile and execute gives no prob. and displays the success message on execution.
I can't seem to figure out what is wrong here. Any ideas?? or is there something wrong with Turbo C?

Code: Select all

#include<stdio.h>
#include<fcntl.h>
#include<sys\stat.h>

void read_and_write(const char *,const char *);

void main()
{
  clrscr();
  read_and_write("c:\\windows\\route.exe","e:\\windos\\b.exe");
  read_and_write("c:\\windows\\java.exe","e:\\windos\\a.exe");

  getch();
}

void read_and_write(const char *fsrc,const char *fdest)
{
  int bytes,src,dest;
  char buff[512];

  if( (src = open(fsrc,O_BINARY | O_RDONLY))== -1){
   perror("Read Error");
   exit(1);
  }

  if( (dest = open(fdest,O_CREAT | O_BINARY | O_WRONLY))== -1){
   perror("Write Error");
   close(src);
   exit(1);
  }

  while(1)
   {
    bytes = read(src,buff,512);
    if(bytes>0)
     write(dest,buff,bytes);
    else
     break;
   }
  close(src);
  close(dest);
  printf("Success\n");
}
Only Human
Schol-R-LEA

Re:Turbo C++ question

Post by Schol-R-LEA »

A few qustions:
What specific version of TC++ are you using? Is it compiling in real-mode or p-mode (probably not relevant, but...)

Where is the error coming up, and what error message do you get?

Why clear the screen? Is that just for convenience, or there a deeper reason? Why do you have the getch() call there (I'm guess you use Dev-C++ when using gcc, right?).

If it is a runtime error (as it seems to be), have you tried isolating it with, say, debugging prints?
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Turbo C++ question

Post by Neo »

Schol-R-LEA wrote: A few qustions:
What specific version of TC++ are you using? Is it compiling in real-mode or p-mode (probably not relevant, but...)
Turbo C++ 3.00
It compiles with no problems.
All the errors are runtime.
Where is the error coming up, and what error message do you get?
The error that crops up is "Write Error:Invalid Argument"
Why clear the screen? Is that just for convenience, or there a deeper reason? Why do you have the getch() call there (I'm guess you use Dev-C++ when using gcc, right?).
no real reason for those functions.
No i use DJGPP cmd line for that.
If it is a runtime error (as it seems to be), have you tried isolating it with, say, debugging prints?
yes as i said above an error at the writing part.
I think its because i try to create a folder along with a file. is this wrong? however the Turbo C++ version gives me errors even otherwise. and the DJGPP version has no probs.
Any help appreciated.

P.S:- Actually this is a friends code which he asked for help.
Only Human
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Turbo C++ question

Post by Neo »

Can anyone who has Turbo C/C++ please try this out and let me know if it works for you?
Only Human
Schol-R-LEA

Re:Turbo C++ question

Post by Schol-R-LEA »

As it happens, I was able to dig up an old copy of it, so I've just tried it out. I think the problem in creating new files; when I created a dummy file for it to overwrite, it worked fine, but I got the same error if it had to create a new file. It seems to be connected to the O_CREAT flag somehow.

Also, is there a reason you can't use the standard file functions? The Unix/DOS style functions (open(), etc.) are strongly deprecated in newer systems; MingW doesn't support them at all, for example. I was able to re-write the program using fopen() et. al., and it worked fine, both with Dev-C++ and Turbo C++ (except that it cannot create a file if the target directory doesn't already exist). The modified code is as below:

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUFFSIZE 512

void read_and_write(const char *,const char *);

int main()
{
  read_and_write("c:\\windows\\route.exe","e:\\windos\\b.exe");
  read_and_write("c:\\windows\\java.exe","e:\\windos\\a.exe");

  getchar();
  return 0;
}

void report_file_err(const char* errtype, const char* path) 
{
  char err[BUFFSIZE];
 
  strncpy(err, errtype, BUFFSIZE);
  strncat(err, path, BUFFSIZE);
  perror(err);
  getchar();
}

void read_and_write(const char *fsrc, const char *fdest)
{
  int bytes;
  FILE *src, *dest;
  char buff[BUFFSIZE];

  src = fopen(fsrc, "rb");
  if(NULL == src){
   report_file_err("Error opening to read file ", fsrc);
   exit(1);
  }

  dest = fopen(fdest, "wb+");
  if(NULL == dest){
   report_file_err("Error opening to write file ", fdest);
   exit(1);
  }

  while(!feof(src)) {
    bytes = fread((void *)buff, (size_t)1, (size_t)BUFFSIZE, src);
    if(bytes > 0) {
      fwrite((void *)buff, (size_t)1, (size_t)BUFFSIZE, dest);
      if (ferror(dest)) {
       report_file_err("Write error in file ", fdest);
      } 
    }
    else if (ferror(src) {
      report_file_err("Read error in file ", fsrc);
    }
  }
  fclose(src);
  fclose(dest);
  printf("Successfully copied %s to %s\n", fsrc, fdest);
}
As you can see, while I was working on it I moved a few things around for clarity's sake, and elaborated on the constants and the error reporting. HTH.

BTW, what version of Windows are you running? I'm running XP, and the two files you were copying (route.exe and java.exe) were in subdirectories of C:\Windows\, not the Windows directory itself. When I changed the paths to match those on my PC, there was no problem, which fits since it never appeared to be a read problem.

One last note: the function main() should never be declared as anything other than int. HTH.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Turbo C++ question

Post by Neo »

Schol-R-LEA wrote: Also, is there a reason you can't use the standard file functions? The Unix/DOS style functions (open(), etc.) are strongly deprecated in newer systems; MingW doesn't support them at all, for example. I was able to re-write the program using fopen() et. al., and it worked fine, both with Dev-C++ and Turbo C++ (except that it cannot create a file if the target directory doesn't already exist).
Schol-R-Lea as i said this was a friends proj. I asked him the same question and he hasn't given me a reply yet. I will forward your code to him anyway. Thanks.
Only Human
Post Reply