User Tools

Site Tools


cs_lang:c

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
cs_lang:c [2011/07/12 10:30] cedriccs_lang:c [2011/07/12 10:37] – [Hello World] cedric
Line 6: Line 6:
  
 ====== Simple examples ====== ====== Simple examples ======
 +===== Hello World =====
 <code c> <code c>
 /* Hello World program */ /* Hello World program */
Line 27: Line 28:
  
 $ ./hello.out  $ ./hello.out 
-Hello Worldcedric +Hello World 
-<code>+</code> 
 + 
 + 
 + 
 +===== Read a file ===== 
 +<code c> 
 +#include <iostream.h> 
 +#include <fstream.h> 
 + 
 +int main () 
 +
 +        //  Open the file 
 +        ofstream inputFile ("./file.txt", ios::in); 
 +        // Test 
 +        if (!inputFile) 
 +        { 
 +                cerr << "Error" << endl; 
 +                exit (1); 
 +        } 
 +        char buf [1024]; 
 +        //  Until there are no more lines in the file, 
 +        //  read and display it. 
 +        while (!inputFile.eof()) 
 +        { 
 +                inputFile.getline (buf,1024);; 
 +                cout << buf << endl;; 
 +        } 
 +        // Close the file 
 +        inputFile.close ();; 
 +
 +</code> 
 + 
 +===== Equivalent in python ===== 
 +<code python> 
 +for line in open("./file.txt", "r"): 
 +    print line 
 +</code>
cs_lang/c.txt · Last modified: 2012/09/30 23:32 by cedric