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

Thread: more files

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

    more files

    heres my problem- this should create a sequenctial file (first program) and then read it (second program). but it doesn't work- instead, when reading the inputted words, it just concatinates them. additionally, it makes a copy of the last word.


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

    void main()
    {
    FILE *fPtr;

    char *word = new char[30];

    if ((fPtr = fopen("a:\\word.dat", "w")) == NULL)
    printf("File could not be opened\n");
    else {



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


    while(!feof(stdin)) { //hit ctrl + z to stop
    cout << "Enter a word: ";
    scanf("%s",word);
    fprintf(fPtr, "%s", word);

    }

    fclose(fPtr);
    }

    delete word;
    }



    //-----------------------------------------------------------------------------
    //-----------------------------------------------------------------------------

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

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

    if((fPtr = fopen("a:\\word.dat","r")) == NULL)
    printf("File could not be opened\n");
    else {

    while(!feof(fPtr)) {
    fscanf(fPtr,"%s", word);
    printf("%s",word);
    cout << "\n";

    }


    fclose(fPtr);
    }

    delete word;
    }



  2. #2
    Join Date
    May 1999
    Location
    Texas, USA
    Posts
    568

    Re: more files

    When you write out the file you need to insert your own carriage return ('\n')

    As in
    fprintf(fPtr, "%s\n", word);



    And in the last function you are using both cout and printf. Both are printing to stdout.

    Wayne


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