CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: files revisited

  1. #1
    Join Date
    May 1999
    Location
    wallingford, pennsylvania
    Posts
    70

    files revisited

    i'm just beginning files, and am in need of help. program makeword (below) simply saves an inputted word to a file. readword should read this file and print out the word, but it doesn't work. help. i already posted this once, and added the suggested changes, but it didn't make any difference. help.



    //program makeword- makes a word and stores it in a file

    #include <stdio.h>
    #include <iostream.h>

    void main()
    {
    FILE *fPtr;


    fPtr = fopen("a:\\word.dat", "w");

    char *word = new char[30];

    cout << "Enter a word: ";
    scanf("%s",word);
    fprintf(fPtr, "%s", word);


    fclose(fPtr);
    }

    //============================================================//



    //program readword- reads data from makeword
    #include <iostream.h>
    #include <stdio.h>

    void main()
    {
    FILE *fPtr;
    char *word = new char[30];

    if((fPtr = fopen("a:\\words.dat","r")) != NULL)
    {
    fscanf(fPtr,"%s", word);
    printf("%s",word);

    fclose(fPtr);
    }
    else { cout << "could not be opened."; }

    }






  2. #2
    Guest

    Re: files revisited

    Could it be that you are writing your word to word.dat and trying to read it from words.dat??


  3. #3
    Join Date
    May 1999
    Location
    CA, USA
    Posts
    586

    Re: files revisited

    Besides the fact (as mentioned earlier) that the two files are named differently, your program will have memory leaks as shown -- remember to delete any memory allocated with new before the pointer variable goes out of scope.

    Rail

    Recording Engineer/Software Developer
    Rail Jon Rogut Software
    [email protected]
    http://home.earthlink.net/~railro/

  4. #4
    Join Date
    May 1999
    Posts
    78

    Re: files revisited


    #include <stdio.h>
    #include <iostream.h>
    void main()
    {
    char *szFile="dummy.dat";
    FILE *fPtr;
    fPtr = fopen(szFile, "w");
    char *word = new char[30];
    cout << "Enter a word: ";
    scanf("%s",word);
    fprintf(fPtr, "%s", word);
    fclose(fPtr);

    if((fPtr = fopen(szFile,"r")) != NULL)
    {
    fscanf(fPtr,"%s", word);
    printf("\nREAD STUFF WAS:%s",word);
    fclose(fPtr);
    }
    else
    {
    cout << "could not be opened.";
    }

    delete word;

    }




    I hust had to make sure the problem had to be that simple and it was


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured