Thread: Writing Garbage data to file ???

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    20

    Writing Garbage data to file ???

    Hi...
    Im programming client/server app that client provide the file name then the server send it to client then the client will save it ..

    this is part of code in client
    Code:
    char buffer[1024];
    printf("FIle is being downloaded ...\n");
                    printf("%s\n",buffer);
                    int fd;
                    fd=open("NewFile.txt",O_WRONLY |O_CREAT| O_APPEND);
                     write(fd,buffer,sizeof(buffer));
    So i have 2 problems ::
    1st one is when i write to file the file permission i cant define it with data type mode_t ,so the file does not open at all after creation...

    2nd one is: the data in buffer is less than 1024 ,the data wrote to buffer but with garbage data . How to make the file read only the real data with garbage ????

    Thanks,

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    22
    1) No idea I've not had much practice with the functions in the POSIX standard.

    2) Don't print the entire buffer? Print until a null terminator.
    Code:
    write(fd,buffer,sizeof(char)*strlen(buffer));

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by zmhnak View Post
    So i have 2 problems ::
    1st one is when i write to file the file permission i cant define it with data type mode_t ,so the file does not open at all after creation...
    Read the man page:
    int open(const char *pathname, int flags);
    int open(const char *pathname, int flags, mode_t mode);
    ...
    O_CREAT ... mode specifies the permissions to use in case a new file is created. This argument must be supplied when O_CREAT is specified in flags; if O_CREAT is not specified, then mode is ignored.
    So what values for "mode" do you supply?

    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    20
    i want the values like this in octal 755 .......

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    man 2 open

    Bye, Andreas

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    2nd one is: the data in buffer is less than 1024
    No, "buffer" is an uninitialised object. Assuming it has automatic storage duration, none of its elements are given values, and trying to get a value from that object without first assigning one makes the behaviour of your program undefined.

    the data wrote to buffer but with garbage data .
    Your code doesn't write anything to "buffer".

    How to make the file read only the real data with garbage ????
    Firstly, files don't read things, so I'll assume you're talking about a program. I don't see why you've specified that it should read only real data -- if you believe your programs are reading imaginary data then I suggest you stop using LSD while programming. Finally, I don't see why you want to do any of that with garbage. Why don't you just forget about playing with garbage and write a program that reads data with C instead?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux IPC - reading/writing files, getting garbage
    By kbfirebreather in forum C Programming
    Replies: 9
    Last Post: 02-01-2009, 02:55 PM
  2. Writing data to file
    By SeyN in forum C Programming
    Replies: 10
    Last Post: 01-07-2006, 06:31 AM
  3. Writing to a Data File
    By joxerjen in forum C++ Programming
    Replies: 6
    Last Post: 10-15-2005, 03:39 AM
  4. i need your help about writing data to file
    By john_tran in forum C++ Programming
    Replies: 3
    Last Post: 10-15-2004, 10:42 AM
  5. Writing Data to a File
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-18-2002, 03:17 AM

Tags for this Thread