Thread: File write

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    34

    Question File write

    I am trying to write to an outpu.dat file from an array called Clients in reverse order

    this is what the function looks like

    int Write_to_Output()
    {
    char Lname,Fname
    int count;
    if ((FilePtr = fopen("Output.dat","w")) ==NULL)
    {
    printf("Error : File cannot open");
    exit(1);
    }
    for (count =4; count=0; count--)
    {

    fwrite(&Record[count], sizeof(struct Clients), 1, FilePtr);
    while (fscanf(FilePtr, "%c", &Lname) !=EOF)








    {
    fscanf(FilePtr,"%c", &Lname) !=EOF)
    printf("%c%c", Lname, Fname);
    }

    fclose(FilePtr);
    return (0);

    Can anyone tell why this generates an unexpected EOF error C1004.

    There seem to be no syntax errors!!!
    I tried to change the count decrement with no success

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    This is a C# board, IE the flagship of MS's .NET framework. You want the C Programming board...

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>generates an unexpected EOF
    means it was still expecting more code when it got to the end of the source file.

    Make sure you have all the necessary brackets and braces in place.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    Ooops!!!

    C,C++,C#, Cflat... its becoming a jingle

    Sorry about that Hammer

    Thanks anyways

    I may or may not be able to do without the lesbians,

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    34

    Read File

    I can't believe that no one knows this

    What would be the command line to read from a text file into an array
    Would it be fread or fwrite

    I know that...
    fscanf would read the disk file
    fread can be used to read random access file
    How would you read into a file? or can you

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>How would you read into a file? or can you
    You don't read into a file, you write to it. Maybe you're looking for fprintf, fwrite, fputs, fputc
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    How you use fputs to write to an array?

    Tried
    fputs(char_name, filePointer);

    This generates an incompatible error

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm confused. You seem to keep changing your story?!

    Start again, and tell me what you're trying to do, and I'll try to help.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    34

    file write

    Nice of you to helpl!!!

    I'm trying to write into an array (random) from a text file(sequential).
    When that is completed
    Write to outputfile(sequential) from array(random) but this time in reversed order.
    Here I would like to print to screen

    I attach the app.

    Much Thanks...

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    34

    file write

    if((FilePtr = fopen("Output.dat","w+b")) == NULL) //open output file//
    {
    printf("Error : File could not be opened.");
    exit(1);
    }

    for (count=5; count>=0; count--)
    {
    fputs(Record[count].Fname,Record[count].Lname, FilePtr);
    printf("%s %s\n",Record[count].Fname,Record[count].Lname);
    }


    fclose(FilePtr);

    return (0) ;

    the code is supposed to open an output.dat file for writing to.
    I have used fputs but keep getting some warnings.. C4133 and C4020.
    the File does appear to be created, and i get no error message
    Anyone know what's happening here

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try this "fixed" version of your code. There are still some things in it I wouldn't recommend, like using a global FILE* variable for one thing. To get it to work, I did these things:

    - Incremented the count variable in the loop in Open_file().
    - Moved the return(0) statement in Open_file(). It was inside the loop.
    - Used fprintf() to do the writing in Write_To_Output().
    - fclose()'d the input file.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    FILE  *FilePtr;
    
    // define 2 dimensional array//
    struct Clients
    {
      char  Lname[15];
      char  Fname[15];
      int   count;
    } Record[5];
    
    int Open_File(void);
    int Write_to_Output(void);
    
    int main(void)
    {
      Open_File();
      Write_to_Output();
      return(0);
    }
    
    int Open_File(void)
    {
      int count;
      count = 0;
    
      if ((FilePtr = fopen("client.dat", "r")) == NULL) //open client.dat as read//
      {
        printf("\n\nError the file could not be opened.");
        exit(1);
      }
    
      while (fscanf(FilePtr, "%s", Record[count].Lname) != EOF)
      {
        fscanf(FilePtr, "%s", Record[count].Fname);
    
        printf("%s %s\n", Record[count].Fname, Record[count].Lname);
        count++;
      }
      fclose(FilePtr);
      return(0);
    }
    
    int Write_to_Output(void)
    {
      int count;
      if ((FilePtr = fopen("Output.dat", "w")) == NULL)
      {
        printf("Error : File could not be opened.");
        exit(1);
      }
    
      for (count = 4; count >= 0; count--)
      {
        fprintf(FilePtr, "%s %s\n", Record[count].Fname, Record[count].Lname);
      }
      fclose(FilePtr);
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM