Files

This is a discussion on Files within the C Programming forums, part of the General Programming Boards category; someone can give me example how to read from file because i tried many ways and none of them work...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    30

    Files

    someone can give me example how to read from file
    because i tried many ways and none of them work

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    PHP Code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>

    #define BUFFER_SIZE 1000

    int main()
    {
        
    FILEFilePointer;
        
    int   Variable;
        
    char  cBuffer[BUFFER_SIZE+1];

        
    Variable 42;

        
    /* open for writing a file named test.txt on C: */
        
    FilePointer fopen"C:\\test.txt""w" );

        
    /* check if successfull */
        
    if( FilePointer == NULL )
        {
            
    printf"Unable to open file for writing..." );
            return 
    1;
        }

        
    /* write to file */
        
    fprintfFilePointer
                 
    "Test Text to test file writing and reading. Variable = %d..."
                 
    Variable );

        
    Variable 18745;


        
    /* write to file */
        
    fprintfFilePointer
                 
    "\nNextLine: Variable = %d..."
                 
    Variable );


        
    /* done, close file */
        
    fcloseFilePointer );

        
    /* now test it, by reading and diplaying it */

        /* open file for reading*/
        
    FilePointer fopen"C:\\test.txt""r" );

        
    /* check if file found*/
        
    if( FilePointer == NULL )
        {
            
    printf"Unable to open file for reading..." );
            return 
    2;
        }

        
    /* while still chars from file unread */
        
    while( ! feofFilePointer ) )
        {

            
    /* read from file */
            
    fgetscBufferBUFFER_SIZEFilePointer );

            
    /* display line from file */
            
    printf"Line read from file was:\n[%s]\n"cBuffer );
        }

        
    /* close file */
        
    fcloseFilePointer );

        
    getch();

        return 
    0;

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    how did u do the syntax highlighting nv?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I used the PHP tag.

    I volunteered in the mod's forum to code another
    highlighting for the CODE tag if i have access to the
    PHP one, because it's colored, but not really C-like.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    can't wait to use the results of that !
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    VS.NET code is colored even when you cut and paste it into programs like notepad or email.
    I compile code with:
    Visual Studio.NET beta2

  7. #7
    Unregistered
    Guest
    Sorry, correct syntax is not FilePointer = fopen( "C:\test.txt", "r" );


    it is: FilePointer=fopen("C:\\test.txt","r");


    Please not the double back slashes in order to avoid representing an escape character.

  8. #8
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    in C# you can use and '@' sign which is a trigger that allows you to use single backslashes in your paths.
    I compile code with:
    Visual Studio.NET beta2

  9. #9
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Hm, I copied that code directly from my VC,
    it should have been correct... at least I do
    have a file named test.txt on my C:\...

    Code:
    backslash test: double backslashes: \\
    PHP Code:
    backslash testdouble backslashes: \\ 
    Lets see if the PHP-tag eats it...


    Oh, WK, I don't think .NET code will be colored
    outside of an editor that supports coloring.
    They would have to write a new notepad to do
    what you promised... However, the @ symbol
    could come in really handy in some situations.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  10. #10
    Witch_king's puppet
    Join Date
    Aug 2001
    Posts
    20
    When you cut and paste it into notepad or Outlook express or other apps it is colored! It retains the color.
    I'm A Farmer

  11. #11
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Not colored in notepad but perhaps XML notepad. It is colored when transfered to Outlook Express or MS Office, wordpad, etc.

    while(true)
    {
    //create a new node used to store a record from the input file
    newp = (node_t *) malloc (sizeof(node_t));
    newp->linkp = NULL;

    doesn't work here though....
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 07:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 04:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 05:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21