C ++ learning in Linux

Source: Internet
Author: User

1. An example of C

(1) text file helloworld. c

# Include <stdio. h>
Void main ()
{
Printf ("Hello world! \ N ");
}

(2)

If helloworld.ois not generated, helloworld.exe is directly generated.

Gcc-O helloworld.exe helloworld. c

 

Generate helloworld.oand then generate helloworld.exe

Gcc-C helloworld. O helloworld. c

Gcc-O helloworld.exe hwloworld. c

 

The executable file a. Out is generated by default.

GCC helloworld. c

(3) Execution

./Helloworld.exe

 

 

 

2. An example of C ++

(1) enter the following content in the text file helloworld. cpp:

# Include <iostream>
Using namespace STD;
Int main ()
{
Cout <& lt; "Hello world! "& Lt; Endl;
Return 0;
}

(2)G ++-O helloworld.exe helloworld. cpp

(3)./helloworld.exe

Note:

(1) the header file is iostream rather than the old version of iostream. h.

(2) Specify the namespace. Otherwise, an error will occur.

(3) Main returns int type, void type will have an error.

(4) Compile with G ++ instead of GCC

(5) If the suffix is CPP, other suffix names may fail.

 

 

3. c ++ calls Shell

(1) enter the following content in the hellosh. cpp text file:

# Include & lt; iostream>
# Include <cstdlib>
Using namespace STD;
Int main ()
{
For (INT I = 0; I <2; I ++)
{
System ("ifconfig ");
}
Return 0;
}

(2) g ++-O hellosh.exe hellosh. cpp

(3)./hellosh.exe

Note:

(1) The system call must contain the header file # include & lt; cstdlib>, not cstdlib. h.

MAN system can be used to check which header file the system must contain. H is not available in the new version.

 

4. Open files and write content in Linux C ++

# Include <iostream>
# Include <fstream>
# Include <iomanip>
Using namespace STD;

Int main ()
{
Ofstream myfile ("myinfo.txt ");

If (! Myfile) return 0; // if an error is returned

Myfile <& lt; SETW (20) & lt; "name:" & lt; "zhangsan" & lt; Endl;
Myfile & lt; SETW (20) & lt; "Address:" & lt; "China" & lt; Endl;

Myfile. Close ();

Return 0;
}

 

5. Read File Content in Linux C ++

# Include & lt; iostream>
# Include <cstdlib>
# Include <fstream>
# Include <iomanip>
Using namespace STD;

Int main ()
{

Fstream myfile ("myinfo.txt ");
Myfile <1234;
Myfile. Close ();

Myfile. Open ("myinfo.txt ");

Int Myint;
Myfile> & gt; Myint;
Cout & lt; Myint & lt; Endl;

Myfile. Close ();
Return 0;

}

 

Note: Invalid operations may cause garbled characters, probably because the file is not properly opened or closed.

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.