copying jpeg files

This is a discussion on copying jpeg files within the C++ Programming forums, part of the General Programming Boards category; I need to make a copy of a jpeg file is there any way that i could be able to ...

  1. #1
    tony kitsch
    Join Date
    Aug 2005
    Posts
    9

    copying jpeg files

    I need to make a copy of a jpeg file is there any way that i could be able to do that. I eventually need to be able to send it through a socket. It would be great if there was some way that i could send any type of file through a socket. If anyone can point me in the right direction, it would be great.

    Thank you

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I'm assuming you mean that you want to do that programatically through C++, because I know you know you can copy + paste...

    you could try opening it as a binary file.
    Code:
    //iostream for standard console I/O
    #include <iostream>
    //fstream for standard file I/O
    #include <fstream>
    
    int main()
    {
    	//used to hold a small part of the file... use sizeof(char) to find out how many
    	//bytes are used on your system.  you can use pretty much any data type here.
    	char ch;
    
    	//open the input file for binary reading
    	std::fstream infile("mitch.jpg",std::ios::binary|std::ios::in);
    	//open the output file for binary writing
    	std::fstream ofile("test.jpg",std::ios::binary|std::ios::out|std::ios::trunc);
    
    	//loop through the file, reading a small part with each iteration
    	while(infile.read(reinterpret_cast<char*>(&ch),sizeof(ch)))
    	{
    		//write that small part to the output file
    		ofile.write(reinterpret_cast<char*>(&ch),sizeof(ch));
    	}
    
    	//explicitly close the input file
    	infile.close();
    	//explicitly close the output file
    	ofile.close();
    	
    	//just a little message
    	std::cout<<"File Written\n";
    	//end of the program
    	return 0;	
    }
    edit: not that you need it, but here's the picture I used: http://www.osei.noaa.gov/Events/Trop...tch299I_G8.jpg
    Last edited by major_small; 08-10-2005 at 05:26 PM. Reason: comments, image linked.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    tony kitsch
    Join Date
    Aug 2005
    Posts
    9
    When i make a copy of the file on one computer it works perfect but when i try to send the characters accross a socket, the data seems to get corrupted. I want to read all the information from a file into a character array and then I want to reassemble it back together on the other end(after sending it through the socket). Text files work perfect but other file types I have a problem, how can i read all of the file data into a character array

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    the same way... except using a char array...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    tony kitsch
    Join Date
    Aug 2005
    Posts
    9
    i am currently doing that but the data still gets corrupted or something, because the contents of the file i copy do not match the original. Should i be using winsock version two rather than version one. Or is there something better and faster than winsock.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Rawr. The TramsmitFile API: http://msdn.microsoft.com/library/de...smitfile_2.asp
    Also, just for concept, here's this extremely biased Windows C code that has printf's and WinAPI calls and other cruel nasty things that I used to do the same thing as you, transfer files, and I mean, I don't even use proper winsock2 function calls. How improper of me! Just std::cout and all the other nifty io/f stream things you want to do in substitution.

    Code:
      while (true)
      {
          if(clientSocket=accept(serverSocket, (struct sockaddr*)&clientSocketAddr, &AddrLength))
          {
              printf("omgclientconnected\n");
              if(hFile=CreateFile("C:\\whatever.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))
              {
                  printf("\tomgwegottohere\n");
                  while(true) {
                      BOOL bResult = ReadFile(hFile, szBuf, 1024, &dwNumRead, NULL);
                      if( bResult == 0 ||  dwNumRead == 0 )
                      {
                          break;
                      }
                      send(clientSocket, szBuf, strlen(szBuf),0);
                      memset(szBuf,0,1024);
                      printf("\troflbuffersent\n");
                  }
                 
              }
              else printf("\tomfgfailure\n");
              printf("\tomgwhatnow\n");
              closesocket(clientSocket);
              CloseHandle(hFile);
          }
      }
    I don't know about better, or faster, I'm sure raw sockets would be enjoyable experience for you, but I would personally just stick to the provided winsock lib's. I don't understand what you mean by this corruption either. I mean, I guess UDP has been renowned for being "less reliable" in that packets are not guaranteed to arrive in the right order or at all, but we should just be using TCP streams here right? Maybe you want to elaborate on your problem?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Nice infinite loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    tony kitsch
    Join Date
    Aug 2005
    Posts
    9
    Can any body give me some sample code that utilizes winsock2 to send messages ( a client - server format). i went to msdn and tried some code but when i ran it, it told me that the bind function failed.

  9. #9
    tony kitsch
    Join Date
    Aug 2005
    Posts
    9
    has any body developed a code that uses the tranmitfile() function, below is

  10. #10
    tony kitsch
    Join Date
    Aug 2005
    Posts
    9
    Has anyone developed code that uses the transmitfile() function that is in the winsock2.h header file. Do i set it it up the same as any other socket.

    Code:
    BOOL TransmitFile(
      SOCKET hSocket,
      HANDLE hFile,
      DWORD nNumberOfBytesToWrite,
      DWORD nNumberOfBytesPerSend,
      LPOVERLAPPED lpOverlapped,
      LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers,
      DWORD dwFlags
    );
    Last edited by adk1283; 08-12-2005 at 09:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 01:59 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  3. Copying Binary Files
    By mikeman118 in forum C++ Programming
    Replies: 9
    Last Post: 08-11-2007, 11:55 PM
  4. OpenCV Library - problems with dll files
    By nipun in forum Windows Programming
    Replies: 3
    Last Post: 11-21-2004, 10:36 AM
  5. using jpeg files
    By phantom in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 01:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21