Thread: read write prob

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    read write prob

    this program gives me the error
    file.c:16: warning: passing arg 1 of `fwrite' makes pointer from integer without a cast

    Here is the code----------------------
    #include <stdlib.h>
    #include <stdio.h>
    #define N 100
    #define mode w
    long j;
    int i;
    main()
    {
    FILE *fp;
    int filename[5];
    filename[5] = (int) rand();
    fp = fopen("filename[5]", "w");
    for (i = 0; i < 100000; i++)
    {
    j = (int) rand();
    if (fwrite(j, sizeof(int), sizeof(long), fp) != 100)
    {
    fprintf(stderr, "Error writing to file.");
    exit(1);
    }
    fclose(fp);
    }
    }

    ----------------------------------

    any suggestions?

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    One of the errors is
    >if (fwrite(j, sizeof(int), sizeof(long), fp) != 100 )
    Fwrite wil never return 100; It should be:
    fwrite( &j, sizeof(long), 1, fp )

    Also i should be of type long, you define mode and N which are never used, you open a file which is called filename[5], there is no variable filename[5] and if it existed you don't use it anywhere [edit]and it should be of type long[/edit] , you don't use your array.

    >any suggestions?
    Change compiler
    Loading.....
    ( Trying to be a good C Programmer )

  3. #3
    Anon Helper
    Guest
    try:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #define N 100
    #define mode w
    int j;
    int i;
    int main() {
      FILE *fp;
      char filename[] = "C:\\test.txt";
      fp = fopen(filename, "wb");
      for (i = 0; i < 100000; i++)
      {
        j = (int) rand();
        if ( (fwrite(&j,      // const void * ptr - Your array
             sizeof(int),     // size of each element in array
             1,                   // number of elements in array
             fp) != 1)         // FILE * stream
        {
           fprintf(stderr, "Error writing to file.");
           exit(1);
        }
      }
        fclose(fp);
    }
    - Put code in code tags.
    - Indicate which line the error belongs to. No one wants to count and we don't know if you have copied exactly.
    - Indent your code.
    - Do a tutorial.
    - Check return value of fopen.
    - Note that this writes values out in binary - not human readable.

    Have Fun!

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Hey, Anon Helper, have you check any of the above answers before posting?
    Loading.....
    ( Trying to be a good C Programmer )

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    thanx for the code. I was really trying to make a randomly generated filename though, thats why i did the whole filename[5] = (int) rand(); thing.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    suggestions

    Hi,
    First i dont know why you have declared mode and N macros. I guess you have plan to use fopen("filename",mode). If you use like this it will not work out. You have to specify as
    #define mode "w".
    The problem is with your filename[5]. It doesn't exist. only filename[4] exists.
    Even then fopen tries to open a file in write mode with filename as filename[5] and not the thing you expected. In order to achieve the thing convert the int to str by sprintf and proceed with the str.
    Error is also in fwrite function. 3 parameter usually specifies no of such elements(1st parameter) you are going to write.
    Why have you specified that 100 and all that stuffs. Really its confusing man.
    Saravanan.T.S.
    Beginner.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I was really trying to make a randomly generated filename though

    The functions tmpnam and tmpfile may be of interest.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read write problem
    By zephon in forum C Programming
    Replies: 3
    Last Post: 01-13-2009, 04:04 AM
  2. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 05:31 PM
  3. using threads to write & read from files
    By kishorepalle in forum C Programming
    Replies: 4
    Last Post: 10-19-2004, 05:19 PM
  4. write(), read()
    By RedRum in forum C++ Programming
    Replies: 5
    Last Post: 06-09-2002, 11:45 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM