C + + Learning Summary 2

Source: Internet
Author: User

Link to the previous log, the following describes the other content in C + +

Add the exception handling code from the previous session:

    Try
    {
        "Try Num" << Endl;
        throw 1.5;
    }
    Catch (double i)
    {
        "catch try num double 1.5" << Endl;
        cout << i << Endl;
    }

VIII. Structure and Consortium

struct Date
{
    int year;
    int Month;
    int Day;
};
Date Catch_today ()
{
    Date oneday;
    CIN >>oneday.year >> oneday.month >> oneday.day;
    return oneday;
}
void Show (Date oneday)
{
    "  " "<< oneday.day << Endl;  
}
int _tmain (int argc, _tchar* argv[])
{
    Date date = Catch_today ();
    Show (date);
    return 0;
}

In the example above, we define the return value of the function as the struct type, and pass the struct to the formal parameter of a function. Of course, we can also pass a partial variable inside a struct to a function as its formal parameter.

struct and pointer:
struct variable pointer, which is used to access all members of the struct variable to which it points. Or, a struct pointer variable.
How to use:
*P-> variable member.
(*p). Variable member.
void Update (S_time *timer);
Call: Update (&SYSTEM);
struct and reference:
struct type name & reference variable name.
struct reference variable. member name.
Test _struct &func (test_struct &var);
{};
Call: K=func (b);
Bit field:
struct test
{
unsigned int a:3;
unsigned int b:4;
unsigned int c:5;
unsigened int d:1;
};
Complex:
Union Band
{
int B1;
Char B2;
Long B3;
};

Consortium: A total of memory, the same as the first address. Generally used in the process of byte stream transmission, the use of alignment.

IX. Documents

File: A collection of data. The data processed by the computer can be persisted only if it is stored in the form of a file to disk.
Classification of files:
Depending on the storage format of the data: a text file and a binary file.
Text file: Also known as the ASC code form or character form, is to store all the data as characters.
Binary files: The way in which data is stored in binary encoding, primarily for encryption.
Depending on how the file is accessed: Sequential files and random files.
Sequential files: Data in a file can only be read sequentially and rewritten. have permission commands, W,r. This is what we usually use.
Random files: Data in a file can be written to or read from a file, regardless of how it is opened.
In C + +, random access and sequential access are not distinguished.
File stream:
In C + +, file stream classes are generally used to process file input and output.
A file stream is a character stream and a binary stream.
Processing Flow:
1. Open a file to associate a file stream object with a disk file.
2. Use the member function of the file stream object to read the data from the file or write the data to a file.
3, close the file, convenient for other programs to perform related operations.
File Stream object:
There are three main categories:
Input stream: Ifstream
Output stream: Ofstream writes the data stream to a file, that is, the view is a file ...
Input/output stream: FStream
Opening and closing of files:
The purpose of opening a file is to establish a variety of information about the file in memory, and to associate the file stream object with the file.
The effect of closing a file is to break the link between the file stream object and the file.
1. Open File
1) define the stream object and open it
Ifstream inf;
Ofstream Outf;
FStream IOF;
The file is opened by using the member function open () to associate the file with the stream.
Inf.open (const char *filename,int fliemode, int access);
2) Open with constructor function
Ofstream outf (const char *filename,int filemode,int access);
where filename is the name of the file we are going to open, or it can be a path name, such as C:\\data\\data.dat
FileMode is the open mode.
Ios::in Open the file as read. iOS:: Out opens files in the way they are written. The Ios::app opens the file as an additional way. Ios::binary accessing files in binary mode
Includes header file "FStream"
This header file contains the above three classes, Ifstream ofstream iostream. They are derived from the IStream ostream Iofstream, and this three is derived from iOS, so ifstream ofstream Iofstream can access all the actions defined by iOS.
Acces Property List:
0: Normal file
1: Read-only files
2: Implied file
4: System files
8: Backing up files
3) Determine if the file is open
Ifstream file1 ("D:\\file.dat");
if (!file1)//file1.fail ()
{
cout << "Failed to open file" <<endl;
}
2. Close the file
Close ();
Outf.close ();
File stream member functions:
Several commonly used member functions are described:
Read (), write ()
This function is used to read and write binary files.
Open () Associates an object that is not initialized with a file.
The put () output stream, which outputs a single character.
EOF () is used to determine if the end of the file is reached
Ignore (20, "") reads 20 characters and ignores spaces.
Peek () Gets the next character in the input stream.
Flush () Refreshes the stream, the benefit of which is to prevent data loss. Because when we perform the output operation again, the data exists in a buffer, and then when it is full, it is written to disk, so it is necessary to flush the operation frequently and then write the data into disk.
Ofstream class
The main is to write the stream data to a disk file, which we can use to create a new file and write the data to an existing file. You can also append a portion of the data to the file.
Ofstream outfile ("Example.txt");
OutFile << "This is a";
Ofstream outfile ("Example.txt", Ios::app);
OutFile << "This is B";
1. Append files
#include <iostream>
#include <fsteam>
using namespace Std;
int main ()
{
cout << "Open output stream file" <<endl;
Ofstream outfile ("Example.txt", Ios::app);
if (!outfile.fail ())
{
cout<< "Append to" <<endl;
OutFile << ", I think!";
}
Else
cout << "Can't open file" << Endl;
return 0;
}
Ifsream class
Reads data from the disk file.
The input stream object that reads the data.
File Random Access
For random access to a file, it is common to first find the location to read and write, and then read or write the data in the call file read-write function.
Example: writes an even number between 2-200 to a binary file, while reading 10 even numbers between 10-19.

int i,j;
Ofstream outfile ("Test1.dat", ios::binary| iOS::out);
   if (Outfile.fail ())
  "Failed to open file"<<endl;
    for (i=2;i<200;i+=2)
  Outfile.write (Char*) &i,sizeof(int));
  Outfile.close ();
Ifstream infile ("Test1.dat", Ios::binary|ios::in);
if (Infile.fail ())
  "Failed to open file"<<endl;
INFILE.SEEKG (20*sizeof(int)); //Move folder pointer
 for (I=0;i<10&&infile.eof (); i++)
{
     Infile.read (Char*) &j,sizeof(int));
    cout << J <<endl;
Infile.close ();

Parse form: 116.111,120,2344,22222 A line of file text file

DWORD WINAPI ChildFunc1 (LPVOID p) {ifstream fin ("Read.txt");stringLine,temp;//line The amount of data in each row, temp is the data that is temporarily loadedHANDLE hevent; hevent = OPENEVENTW (event_all_access,false,_t ("ChildEvent1"));intCountnum = 0; while(Getline (Fin,line))//File not to end{countnum + +;if(Countnum = = 4) {countnum = 0;//Eight-wire lidar two frames for one cycle            if(Recvcount1 = = 6)            {recvcount1 = 0; }intStrnum = 0; for(inti = 0; I < line.size (); ++i) {if(Line[i] = =', ') {StringStream stream; Stream << temp;//temp need to clear 0                Switch(Strnum) { Case0:stream >> Szbuffer1[recvcount1].lon;stream.str (""); temp =""; Break; Case1:stream >> Szbuffer1[recvcount1].lat;stream.str (""); temp =""; Break; Case2:stream >> Szbuffer1[recvcount1].speed;stream.str (""); temp =""; Break;default: Break;            } strnum++; }Else{temp + = Line[i]; }if(i = = Line.size ()-1)                {StringStream stream; Stream << temp;//temp need to clear 0Stream >> Szbuffer1[recvcount1].pathangle; STREAM.STR (""); temp =""; }} cout <<"Flag1:"<<recvcount1<<endl;        recvcount1++;        SetEvent (hevent);      Sleep (200); }    }return0;}
Parsing a binary file: reading a struct
DWORD WINAPI Childfunc (LPVOID p)
{
    FILE *infile = fopen ("Data.txt","RB"
    long i = 0;
    HANDLE hevent;
    hevent = OPENEVENTW (event_all_access,false,_t ("childevent"));
     while (!feof (infile)) //File not to end
    {
        Eight-wire LiDAR two frames for a one-cycle period
        Fseek (infile,i*sizeof(KeyPoint), seek_set); //offset from the beginning of the file
        ++i;
        Fread (&szbuffer[recvcount],sizeof(KeyPoint), 1,infile); //read one  frame at a time
        "flag:  "<<recvcount<<endl;
        recvcount++;
        if (Recvcount = = 6)
        {
            Recvcount = 0;
        }
        SetEvent (hevent);
        Sleep (80);
    }
    return 0;
}

The main problem is locating trouble, the use of fseek functions, continuous reading

Related Article

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.