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.