cs_lang:c
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cs_lang:c [2011/07/12 10:37] – [Hello World] cedric | cs_lang:c [2011/07/12 10:47] – [Equivalent in python] cedric | ||
---|---|---|---|
Line 35: | Line 35: | ||
===== Read a file ===== | ===== Read a file ===== | ||
<code c> | <code c> | ||
- | #include <iostream.h> | + | #include <stdio.h> |
- | #include < | + | |
+ | FILE *fr; /* declare the file pointer */ | ||
+ | |||
+ | main() | ||
- | int main () | ||
{ | { | ||
- | // Open the file | + | char line[80]; |
- | | + | |
- | | + | fr = fopen (" |
- | if (!inputFile) | + | /* "rt" |
- | { | + | |
- | cerr << | + | |
- | exit (1); | + | |
- | } | + | printf |
- | char buf [1024]; | + | |
- | // Until there are no more lines in the file, | + | fclose(fr); |
- | // | + | |
- | while (!inputFile.eof()) | + | |
- | { | + | |
- | | + | |
- | cout << buf << endl;; | + | |
- | | + | |
- | // Close the file | + | |
- | inputFile.close ();; | + | |
} | } | ||
</ | </ | ||
- | ===== Equivalent in python | + | ==== Equivalent in python ==== |
<code python> | <code python> | ||
for line in open(" | for line in open(" | ||
print line | print line | ||
</ | </ |
cs_lang/c.txt · Last modified: 2012/09/30 23:32 by cedric