English 中文(简体)
需要帮助将c转换为++(简单错误,但可以固定)
原标题:need help converting c to c++ (simple error but cant fix)
  • 时间:2012-01-11 23:31:37
  •  标签:
  • c++

I have a c++ homework. The homework is asking to convert a c program to c++. Below is the question:

You are requested to convert the following C function into a C++ function and then embed it into a complete program and test it. Note that this function copies a binary file of integers and not a text file. The program must accept the arguments (the file to copy and the file to be copied to) from the command line.

/* ==================== cpyFile =====================

This function copies the contents of a binary file
of integers to a second file.
Pre fp1 is file pointer to open read file
fp2 is file pointer to open write file
Post file copied
Return 1 is successful or zero if error
*/
int cpyFile (FILE *fp1, FILE *fp2)
{
  /* Local Definitions */
  int data;

  /* Statements */
  fseek (fp1, 0, SEEK_END);
  if (!ftell (fp1))
  {
    printf ("
acpyFile Error : file empty

");
    return 0;
  } /* if open error */
  if (fseek (fp1, 0, SEEK_SET))
    return 0;
  if (fseek (fp2, 0, SEEK_SET))
    return 0;

  while (fread (&data, sizeof (int), 1, fp1))
    fwrite (&data, sizeof (int), 1, fp2);
  return 1;
} /* cpyFile */

I did my best and managed to convert it, but unfortunately when I m using it , the file that I get after the copy is empty. Below is my answer:

#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc,char* argv[])
{
  if(argc!=3)
  {cerr<<"invalid number of arguments. must be 3."<<endl;exit(1);}

  fstream fp1(argv[1],ios::in);
  if(!fp1)+{cerr<<argv[1]<<" could not be opened"<<endl;exit(1);}

  fstream fp2(argv[2],ios::out);
  if(!fp2)+{cerr<<"file could not be found."<<endl;exit(1);}


  int data;

  fp1.seekg (0,ios::end);
  if (!fp1.tellg ())
  {
    cout<<"
acpyFile Error : file empty

";
    return 0;
  } /* if open error */
  if (fp1.seekg (0, ios::beg))
    return 0;
  if (fp2.seekg (0, ios::beg))
    return 0;

  while (fp1.read (reinterpret_cast<char*>(&data), sizeof (int)))
  {
    fp2.seekp(0);
    fp2.write (reinterpret_cast<char*>(&data), sizeof (int));
  }
  return 1;
}

我尽了最大努力,但我只读了双亲文件,而我没有想到什么。

最佳回答

你们需要像其他人所说的那样,以双轨方式打开档案。

fstream fp1(argv[1], ios::in | ios::binary); // combine ios::in with ios::binary

fstream fp2(argv[2], ios::out | ios::binary); // combine ios::out with ios::binary

或您可将其编成ifstream ( in file stream forread only) and (out file stream, for written only)并删除ios:in and ios:out:>;>是因为ifstream <>>>:in s>>。

ifstream fp1(argv[1], ios::binary);

ofstream fp2(argv[2], ios::binary);

You need to do this because if you don t, the file will be translated when you read from or write to it for things like turning line endings from or to just , etc, which will mess up your binary data which may happen to have those bytes in them.

这是:

if (fp1.seekg (0, ios::beg))
    return 0;

if (fp2.seekg (0, ios::beg))
    return 0;

由于<编码>见千克 回归你称之为的物体,将始终使你的代码回归。 在这方面,它并不等同于<代码>fseek ,因为fseek 成功回报0。 因此,你从来不去上<代码>。 采用<条码>的代码(<>)

fp1.seekg(0, ios::beg);
fp2.seekg(0, ios::beg);

或者,如果你必须检查,你想要这样做。

if (!fp1.seekg (0, ios::beg)) // notice the added !
    return 0;

if (!fp2.seekg (0, ios::beg)) // notice the added !
    return 0;

此外(在<代码>上,):

fp2.seekp(0);

问你要写到档案开始的时候。 因此,你在案卷一开始就永远不会写任何东西。 完全删除这条线。

Also, 您在座右铭上有一个return,使座右铭返回。 http://www.un.org/Depts/DGACM/index_french.htm 这一点从来都不会被人误解,因为不寻常的血腥风格。

问题回答

每当你从<代码>fp1读出一个新的数据组时,你就将<代码>fp2重新排入到下游,基本上放弃了你已经写到的<编码>fp2的内容。 电话:fp2.seekp(0)走出主机。

你有几个问题。 一、通过确定这一界限:

if (fp1.seekg (0, ios::beg))
    return 0;
if (fp2.seekg (0, ios::beg))
    return 0;

<编码>见千克。 这种方法可检索<代码>istream。

fp1.seekg (0, ios::beg);
if (fp1) // i.e., if fp1 is in a valid state (as opposed to e.g. end-of-file)
    return 0;
fp2.seekg (0, ios::beg);
if (fp2) // i.e., if fp2 is in a valid state (as opposed to e.g. end-of-file)
    return 0;

这显然不是你想要的。

为修改你的代码,您可使用以下表格:std:cout << “Got to line “ << __LINE__ << 编:endl;,以图显示方案的某些部分实际运行。 这将迅速发现上述问题。

普通档案需要以双轨方式特别开放,因此,如果您有<条码>fstream fp1(argv ***ios:in);。 您还应增加一篇文章:如:fstream fp1(argvl)

在C++代码中,你试图在撰写每份产出文件之前先开始编制产出文件,因此产出文件最长不超过2份。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?