Re: Parsing CSV files
Tom Serface wrote:
I think Joe is saying it is meaningless these days because there is no
carriage to return any longer. I think most of us consider \n
synonymous with Enter and that implies the start of a new line. A lot
of this is carry over from the days of teletype and paper terminals and
we're just stuck with it as part of ASCII.
I just wanted to add, yes, \n is viewed as a new line, but that is
only in the DOS/Windows world. Not the case in the "other" worlds!
In the DOS/Windows programming the default is COOKED mode when you
open a text file. COOKED means it will do translations for you - in
both directions. In RAW mode, there is no translation and you must be
specific, <CR><LF> or \r\n.
In MS C/C++, file I/O runtime library function
_setmode()
can be used to set/change the binary (RAW) or text (COOKED)
translation mode. For example,
_setmode( _fileno( stdin ), _O_BINARY );
_setmode( _fileno( stdout ), _O_BINARY );
will set a standard I/O console program to be compatibility with the
UNIX/MAC/DOS world because you are dealing with RAW bytes, no
transparent translations being done.
Here is a quick portable "fetch" program you can use to GET a HTTP
resource from a web site:
================= CUT HERE ======================
/* fetch.c -- fetch via HTTP and dump the entire session to stdout
very stupidly. Illustrate need to change the stdout
default _O_TEXT cooked mode to _O_BINARY raw mode.
*/
#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <winsock.h>
#include <fcntl.h>
#include <io.h>
#pragma comment(lib,"wsock32.lib")
#define close(a) closesocket(a)
#define read(a,b,c) recv(a,b,c,0)
#define write(a,b,c) send(a,b,c,0)
#else
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#endif
main(argc, argv)
int argc;
char **argv;
{
int pfd; /* fd from socket */
int len;
char *hostP, *fileP;
char buf[1024];
struct hostent *hP; /* for host */
struct sockaddr_in sin;
#ifdef _WIN32
WSADATA wd;
if (WSAStartup(MAKEWORD(1, 1), &wd) != 0) {
exit(1);
}
_setmode( _fileno( stdin ), _O_BINARY );
_setmode( _fileno( stdout ), _O_BINARY );
#endif
if ( argc != 3 ) {
fprintf( stderr, "Usage: %s host file\n", argv[0] );
exit( 1 );
}
hostP = argv[1];
fileP = argv[2];
hP = gethostbyname( hostP );
if ( hP == NULL ) {
fprintf( stderr, "Unknown host \"%s\"\n", hostP );
exit( 1 );
}
pfd = socket( AF_INET, SOCK_STREAM, 0 );
if ( pfd < 0 ) {
perror( "socket" );
exit( 1 );
}
sin.sin_family = hP->h_addrtype;
memcpy( (char *)&sin.sin_addr, hP->h_addr, hP->h_length );
sin.sin_port = htons( 80 );
if ( connect( pfd, (struct sockaddr *)&sin, sizeof(sin) ) < 0 ) {
perror( "connect" );
close( pfd );
exit( 1 );
}
sprintf( buf, "GET %s HTTP/1.0\r\n"
"host: %s\r\n"
"accept: *.*\r\n\r\n", fileP, hostP);
write( pfd, buf, strlen(buf));
while ( ( len = read( pfd, buf, sizeof(buf)) ) > 0)
fwrite( buf, 1, len, stdout );
close( pfd );
fflush( stdout );
exit( 0 );
}
================= CUT HERE ======================
--
HLS