A summary of the experience of C + + file operation

Source: Internet
Author: User

Recently, a rectangular element (x, Y, W, h) that is stored in rows is read from Groundtruth_rect.txt, and the text is stored in the following format:

310,102,39,50308,100,39,50306,99,39,50306,98,38,49304,97,38,49303,96,37,48
generally dealing with this kind of formatted storage data, my favorite is the C language formatted output and input fprintf and fscanf, because know the format, so a loop can be repeated from the TXT to extract the rectangle, very simple and convenient. But when I define a file* file = fopen ("Groundtruth_rect.txt", "R"), when the program always has a bugger, the compiler (VS2010) output error message is:
error C2679: Binary "=": operator not found to accept right operand of "FILE *" type (or no acceptable conversion) 1> C:\Program Files (x86) \microsoft Sdks\windows\v7.0a\include\winbase.h (6618): Could be "_win32_find_dataa & ; _win32_find_dataa::operator = (const _WIN32_FIND_DATAA &) "
transferred to the tune and did not find the cause of the error, on the internet did not find the relevant tips. I guess the reason for this is that it is defined in the program and contains something to make it easier for me to get the path to the sequence image under the folder:
#define _afxdll
#define _WIN32_WINNT 0x0502
#include <WinBase.h>
#include <ShlObj.h>
#include <afxdlgs.h>
#include <atlstr.h>


(if there is a "fellow-human" want to be able to pointing twos)
If you get rid of that part of the bugger, all of it disappears. Because it must be used to get the path, so we have to abandon the use of file * C-language files operation, instead of using C + + provides a file operation method:
String line;
Getline (file, line);
Istringstream Linestream (line);
String x1, y1, W1, H1;
Getline (Linestream, X1, ', ');
Getline (Linestream, Y1, ', ');
Getline (Linestream, W1, ', ');
Getline (Linestream, H1, ', ');
int x = atoi (X1.c_str ());
int y = atoi (Y1.c_str ());
int w = atoi (W1.c_str ());
int h = atoi (H1.c_str ());
Groundtruth_rect[i] = rect (x, Y, W, h);

The above code is a good way to run without errors. The use of the full character to treat, with ', ' as the end of the line getline, a row of reading data, and then use Atoi () to convert the string into a number, it also realized the purpose of formatting reading data. Let 's give an example of using Getline
<span style= "FONT-SIZE:18PX;" >//string_getline_sample.cpp//compile with:/ehsc//illustrates-use the Getline function to read a//line of T Ext from the keyboard.////functions:////    getline       Returns A string from the input stream./////////////////////// #pragma warning (disable:4786) #include <string> #include < iostream>using namespace std; int main () {   string S1;   cout << "Enter A sentence (use <space> as the delimiter):";   Getline (CIN,S1, ");   cout << "you entered:" << s1 << Endl;;} </span>

then give an example of the use of Atoi (): Atoi converts a string that can be interpreted as a number into shaping data, from the beginning of a string to the first character that cannot be interpreted as a number, such as "s".
<span style= "FONT-SIZE:18PX;" >//crt_atoi.c//This program shows what numbers//stored as strings can be converted to//numeric values using the ATO I functions. #include <stdlib.h> #include <stdio.h> #include <errno.h>int main (void) {    char    * str = NULL;    int     value = 0;    An example of the Atoi function.    str = "  -2309";    Value = atoi (str);    printf ("Function:atoi (\"%s\ ") =%d\n", str, value);    Another example of the Atoi function.    str = "31412764";    Value = atoi (str);    printf ("Function:atoi (\"%s\ ") =%d\n", str, value);    Another example of the Atoi function     //with an overflow condition occuring.    str = "3336402735171707160320";    Value = atoi (str);    printf ("Function:atoi (\"%s\ ") =%d\n", str, value);    if (errno = = Erange)    {       printf ("Overflow condition occurred.\n");}    } </span>

The output is:
Function:atoi ("  -2309") = -2309function:atoi ("31412764") = 31412764function:atoi ("3336402735171707160320") = 2147483647Overflow condition occurred.


but at first did not think of this method, but the use of a relatively stupid approach, just start to read the data correctly, but read half of the time actually read the data is 0, I am really fast (I would like to use the format of C in the input is good, the results of two hours before the fix, Mood depressed imaginable)
int test[4];

Test[0] = 0;
file>>dec>>test[0];
groundtruth_rect[i].x = test[0];

TEST[1] = 0;
FILE.SEEKG (1,ios::cur);
file>>dec>>test[1];
GROUNDTRUTH_RECT[I].Y = test[1];

TEST[2] = 0;
FILE.SEEKG (1,ios::cur);
file>>dec>>test[2];
Groundtruth_rect[i].width = test[2];

TEST[3] = 0;
FILE.SEEKG (1,ios::cur);
file>>dec>>test[3];
Groundtruth_rect[i].height = test[3];
//file.seekg (1,ios::cur);

The method used is the operator >>, and the middle Dec is the way to represent the input in decimal mode, the insertion (>>) + operator (DEC), just beginning without adding FILE.SEEKG (1,ios::cur); In addition to the first reading of the data is all 0, I step through the debugging (not too familiar with this mechanism, and the urgency to achieve, can only elephant). Later, FILE.SEEKG (1,ios::cur) was used to locate the read position of the file, skipping the comma of the formatting function, so that it could be read in correctly. After the results began to run, found that there are more than 500 rectangles of data, this way can only read to 221, and read out the data is right, but the back of more than 200 do not know why read not come in. This bugger also still did not solve (hope to have expert pointing maze)!
with the above debugging experience, I feel that can no longer elephant, then decided to C + + typical file operation to learn and summarize:
specific content can be seen in other people's blog, I am only the more common content described below: http://blog.csdn.net/jiangxinyu/article/details/7875931 and/http Www.cnblogs.com/kzloser/archive/2012/07/16/2593133.html

whether it is Linux (shell) or Windows, the general default open input objects are three kinds: one is the standard input device keyboard buffer, the second is the standard output device monitor buffer, three is the error message. (especially in the case of Linux shell commands, very classical control, it is interesting to understand that the input and output redirection method is similar to C + + iostream and Fstream,ofstream,ifstream).
of course, the first is to open the file, generally will have text files and binary options, the default is mostly text files, C language "wb+" B is binary, and C + + for iOS::binary C + + hasoperator function input/output
Dec formatted as decimal numeric data input and output
Endl outputs a newline character and refreshes the stream output.
ends output A null character output
Hex formatted as hexadecimal numeric data input and output
Oct formatted as octal numeric data input and output
setpxecision (int p) Sets the precision bit output of a floating-point number
corresponds to a formatted input output in C:%c,%d,%f,%s and so on.
In addition to the C file operation is not the same way, the C + + management read pointers and write pointers, respectively, can use SEEKG and SEEKP to set Ios::beg: File Start
Ios::cur: File Current Location
Ios::end: End of File
and the corresponding to C is fseek (), Ftell and so on. the file operations of C are summarized as follows:

C + + Classic file operation is as follows:


*********************************************************************************************************** 2015-7-24

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A summary of the experience of C + + file operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.