Thread: C programming reading/writting query

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    14

    C programming reading/writting query

    Hello guys,

    Just got a question regarding my program which user inputs a file and I read content inside and create exact copy of file, with the same name.

    I have got it working fine for copying the data (content text) along with the filename/filesize before. However I have slight problem

    Code:
    fdin = open(argv[2].......
    fdout=open("copyfile",.....
    
    namelength = strlen(argv[2]);
    write (fdout, (unsigned char*)&namelength, sizeof (unsigned char));
    write (fdout, argv[2], sizeof(namelength));
    
    int bytesread = read(fdin, buffer, sizeof(buffer)))
    
    write(fdout,bytesread,sizeof(bytesread);
    write(fdout,buffer,sizeof(bytesread);
    So, say user inputted a file called "hello.txt"-argv[2] and the namelenght variable would be 9 and say if the content data was "This is the file"-(content would be 16 bytes long)

    Then the copy file should look like this, just a long string:
    9hello.txt16Thisisthefile

    Thus I can easily using read() function read the first byte(unsigned char) returning value 9 so I now read the next 9 bytes to get full file name back (hello.txt) then read the next byte being (int type) returning 16 (so I can read next 16 bytes to get content data back)

    My problem is the content (data text part 16 bytes) writes ok but all the stuff before (mainly number values) is blank? I understand you cant view integer values something to do with being converted in binary format?

    Thanks if any one can help me out

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You are casting the address of namelength as an indirect pointer to an unsigned char. You'll instead need to convert the integer that is contained in namelength to an ascii value in an intermediate field, and then output that converted value.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to make a choice: if you use write, you know the number will be one sizeof-number long, but you yourself will never be able to see the number if you visually inspect the file. If you use fprintf or similar to print it as a string, you'll be able to see what the number is yourself, but you won't know when to stop. (I.e., if you see "58ff", does that mean that you have five characters, with the data starting with an "8", or fifty-eight characters, with the data starting with an "f"?)

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by daza166 View Post
    Then the copy file should look like this, just a long string:
    9hello.txt16Thisisthefile
    As the others have pointed out this is going to be a problem if you ever intend to recover the data...

    If your input file is named 12.txt
    and contains 1234567890

    your result file would be
    612.txt101234567890

    And how do you know where one thing ends and the next begins?

    You're going to have to add some punctuation so it is clear what is what. Something like...
    6,12.txt,10,1234567890

    Which lets you use the comas as delimiters to read the data back in formatted input statements....

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Quote Originally Posted by CommonTater View Post
    You're going to have to add some punctuation so it is clear what is what. Something like...
    6,12.txt,10,1234567890
    Which lets you use the comas as delimiters to read the data back in formatted input statements....
    Thanks for reply guys,
    So what would be the best way to go, as I can not visually see numbers so how do I know if the program is copying files correctly? delimiters may be the way to go, but I still want to learn using the write() function NOT fwrite().

    Could you give me an example if possible of how I could use write(), read() & delimiters, so I can correctly read() positions of data, in order to copy file correctly and possibly see output so I know whats happening?

    Would be very helpful, been stuck on this for days.

    Many Thanks

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't understand your need to print out the length of the file - use ftell() and two pointers instead (or unsigned longs), to give you that info. You know that a char is limited in range to a max of 256?

    That's not a very long file.

    I wouldn't use comma's as delimiters, I'd use one space.

    fc or diff command followed by the file names, will let the system confirm whether the files are the same, or different. I'm not familiar with diff, but fc is used in Windows and has several options:

    fc /? gets info, in any console window.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by daza166 View Post
    Thanks for reply guys,
    So what would be the best way to go, as I can not visually see numbers so how do I know if the program is copying files correctly?
    If fread reports the right number, then you wrote it out correctly.

    If you don't trust your fread skills, you can open the file in something that will show you hex codes (a hex editor, say, or just a regular editor that has a hex mode) and look at the bytes of the integer (one byte for the first one, since that's just a char; probably four bytes for the second one that's an int). For the last one, keep in mind that your machine's endianness will affect the value -- for instance, you'll probably see 16 as 10 00 00 00 (lower bytes first), rather than 00 00 00 10.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2009, 03:43 PM
  2. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  3. query problem
    By arian in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  4. DNS Query
    By Simpsonia in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-24-2006, 12:42 AM
  5. dns query
    By bulldog in forum C Programming
    Replies: 6
    Last Post: 02-24-2004, 10:44 AM