/* chorder.c, Sept 2007, specifically for altering the order of columns in a file produced by prog8.c, intended for Hectospec stuff. */ #define O_BINARY 0 /* (Above) O_BINARY must be 0 for unix or linux. For windows, however, comment this statement out. */ #define MAXSIZE 1000000 /* size of input array, also the output array */ #include #include #include main() { char str[4000],filename1[42],filename2[42]; char *buffer1,*buffer2,colval[25][124]; int i,j,k,l,m,n,o,p,q; int fd,filesize1a,filesize1b,filesize1c,filesize2,maxsize; int hdrlines,startp,p1,ncols1,coln,seq[25],ncols2,linenum; int nbadrows,okrow; buffer1 = malloc(MAXSIZE); if (buffer1 == NULL) { printf("\n*** Can\'t allocate %d bytes for buffer1[] ***\n",MAXSIZE); exit(0); } buffer2 = malloc(MAXSIZE); if (buffer2 == NULL) { printf("\n*** Can\'t allocate %d bytes for buffer2[] ***\n",MAXSIZE); exit(0); } printf("\n\n\n-------\n\n O_BINARY = %d. This should be 0 for a "); printf("linux/unix computer,\n but something else for a windows "); printf("computer. If incorrect, change\n the \"#define O_BINARY 0\" "); printf("near the beginning of the source code,\n and recompile. "); printf(" IF O_BINARY IS OK, HIT TO CONTINUE : "); gets(str); if (str[0] == 'q') exit(0); fd = -1; while (fd < 0) { printf("\nInput file (ascii text file) : "); gets(str); if (str[0] == 'q' && str[1] == '\0') exit(0); for (i=0;i<40;i++) filename1[i] = str[i]; filename1[40] = '\0'; fd = open(filename1,O_BINARY|O_RDONLY); if (fd < 0) printf("\n*** Can\'t open \"%s\" ***\n",filename1); } filesize1a = read(fd,buffer1,MAXSIZE); close(fd); fd = -1; printf("Found %d bytes in \"%s\".\n",filesize1a,filename1); if (filesize1a >= MAXSIZE) { printf("\n*** File is too large for buffer; increase MAXSIZE in source code ***\n"); exit(0); } if (filesize1a < 100) { printf("\n*** We expect at least 100 bytes in the file. ***\n"); exit(0); } /* Replace tab characters with space characters ' ': */ n = 0; for (p=0;p to continue : "); gets(str); if (str[0] == 'q') exit(0); m = 1000; if (filesize1c < m) m = filesize1c; sprintf(str,">|"); j = 2; for (p=0;p 1) { sprintf(&str[j],">|"); j = j + 2; } } str[j] = '\0'; printf("\nFirst %d bytes of the file :\n\n%s\n",m,str); /* ===-=== */ k = 0; while (k != 1) { printf("\nSkip how many text lines? "); gets(str); if (str[0] == 'q') exit(0); k = sscanf(str,"%d",&hdrlines); if (hdrlines < 0) k = 0; } startp = 0; if (hdrlines > 0) { n = 0; for (p=0;p= 120) { printf("\n*** More than 120 characters in a column ***\n"); exit(0); } if (coln >= 24) printf("\n*** Warning: max 24 columns ***\n"); if (coln >= 24 || buffer1[p] == 10) break; } ncols1 = coln; if (ncols1 < 2) { printf("\n*** Need at least two columns ***\n"); exit(0); } printf("\nFound %d columns; entries in first row are",ncols1); for (coln=0;coln ncols1) { ncols2 = 0; printf("\n*** All entries must be in range 1--%d ***\n",ncols1); } --seq[i]; } } /* ensure that the last character is a line break: */ if (buffer1[filesize1c-1] != 10) buffer1[filesize1c++] = 10; /* ===-=== */ p1 = startp; nbadrows = 0; filesize2 = 0; for (linenum=0;p1= 120 || coln >= 24) { okrow = 0; for (i;i= MAXSIZE) { printf("\n*** Output is incomplete, filled buffer2[] ***\n"); okrow = 0; break; } } if (!okrow) break; } buffer2[filesize2++] = ' '; } if (!okrow) /* don't substitute "else" */ { ++nbadrows; sprintf(&buffer2[filesize2],"BAD ROW "); filesize2 = filesize2 + 8; } buffer2[filesize2++] = 10; /* end of line */ } /* linenum-loop */ printf("\nHave copied approximately %d text lines.\n",linenum); close(fd); fd = -1; while (fd < 0) { printf("\nOutput file (new ascii file) : "); gets(str); if (str[0] == 'q' && str[1] == '\0') { printf("\n*** To avoid accidental loss of work, \'q\' exit doesn\'t work here. ***\n If you really want to quit with no output, use ctrl-C.\n"); } else { for (i=0;i<40;i++) filename2[i] = str[i]; filename2[40] = '\0'; k = open(filename2,O_BINARY|O_RDONLY); if (k >= 0) { printf("\n*** \"%s\" already exists ***\n",filename2); close(k); } else { fd = open(filename2,O_CREAT|O_EXCL|O_BINARY|O_WRONLY,0666); if (fd < 0) printf("\n*** Can\'t open \"%s\" ***\n",filename2); } } } o = write(fd,buffer2,filesize2); if (o != filesize2) printf("\n*** Write error ***\n"); close(fd); printf("\n--- Have created new file \"%s\" ---\n",filename2); } /* ----- end of program ----- */