English 中文(简体)
案文档案中相应部分的阅读
原标题:Reading of sequential parts from a text file
  • 时间:2012-01-13 18:03:54
  •  标签:
  • c++
  • file-io
    int fread(char cc[],int a[],int q)
    {
        ifstream infile;
        infile.open("C:\Dev-Cpp\DCS\Decom\a.txt",ios::binary);
        infile.read(cc,257);
        infile.close(); 

        for(int i=0;i<256;i++){a[i]=(unsigned char)cc[i];}
        return 0;
    }

现在,q 从表面反面获得价值,这里是我想要做的事,

if q is 0, it reads the first 256 characters and if q is 1, it reads the characters from position 256 to 512 and so on

这样做的正确方式,取决于并具有一系列规模256,或者是否有更有效的方法这样做?

        int fread(char cc[],int a[],int q)
        {
            int ka=0,kb=0;
            kb=q*256; 
            ka=ka+256;
            ifstream infile;
            infile.open("C:\Dev-Cpp\DCS\Decom\a.txt",ios::binary);
            infile.seekg(256);
            infile.read(cc,ka);
            infile.close(); 

            for(int i=0;i<256;i++){a[i]=(unsigned char)cc[i];}
            return 0;
        }

I m getting a Unhandled exception at 0xb9fb2475 in s949.exe: 0xC0000005: Access violation reading location 0xb9fb2475 when i debug it in vc++ 2010.

最佳回答

Your second version of fread is close. Here are some issues I identified.
1. The title is fread. This is a common C and C++ library function. Could present problems down the road, when fread is called (which version would they be using?).
2. When passing arrays, the capacity of the array should be passed also.
3. When passing arrays as source, declare them as const.
4. When passing arrays as the target / destination, pass by reference.
5. Don t use arrays, std::vector is a lot safer.
6. When position the input file, use your value not a constant 256.
7. Include the size of the record when determining positions. If you are reading integers, multiply by sizeof(int).
8. Why read into a local buffer then copy to the client s buffer when you can directly read into the client s buffer?
9. Prefer library routines, such as std::copy, when copying the data.

问题回答

暂无回答




相关问题
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?

热门标签