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 revisionBoth sides next revision
cs_lang:c [2011/07/12 10:37] – [Hello World] cedriccs_lang:c [2011/07/12 10:47] – [Read a file] cedric
Line 35: Line 35:
 ===== Read a file ===== ===== Read a file =====
 <code c> <code c>
-#include <iostream.h> +#include <stdio.h>   /* required for file operations */ 
-#include <fstream.h>+ 
 +FILE *fr;            /* declare the file pointer */ 
 + 
 +main()
  
-int main () 
 { {
-        //  Open the file +   char line[80]; 
-        ofstream inputFile ("./file.txt", ios::in); + 
-        // Test +   fr = fopen ("./file.txt", "rt");  /* open the file for reading *
-        if (!inputFile) +   /* "rtmeans open the file for reading text */ 
-        { + 
-                cerr << "Error<< endl; +   while(fgets(line, 1024, fr!= NULL
-                exit (1); +   
-        } +         printf ("%s\n"line); 
-        char buf [1024]; +   
-        //  Until there are no more lines in the file, +   fclose(fr);  /* close the file prior to exiting the routine */
-        //  read and display it. +
-        while (!inputFile.eof()) +
-        +
-                inputFile.getline (buf,1024);+
-                cout << buf << endl;; +
-        +
-        // Close the file +
-        inputFile.close ();;+
 } }
 </code> </code>
cs_lang/c.txt · Last modified: 2012/09/30 23:32 by cedric