Hello,
I have written a simple program to convert a tab delimited file into a fixed length file. It works for the most part but the results are no what I expect. There are 16 fields and it prints all 16 fields but all the fields past the second one have the same info in them. Example: A file with the info:
ref - REC00004A1
clNum - hosway
clName - wayne general
acct - 100023
The first field would print as REC00004A1, the second field would print -hosway- and all swubsequent fields will also print -hosway- no matter what I try. I hope I have explained my problem well enough. Can anyone help? Here is my code:
I have written a simple program to convert a tab delimited file into a fixed length file. It works for the most part but the results are no what I expect. There are 16 fields and it prints all 16 fields but all the fields past the second one have the same info in them. Example: A file with the info:
ref - REC00004A1
clNum - hosway
clName - wayne general
acct - 100023
The first field would print as REC00004A1, the second field would print -hosway- and all swubsequent fields will also print -hosway- no matter what I try. I hope I have explained my problem well enough. Can anyone help? Here is my code:
Code:
// this program will open a tab file and save as fixed length
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
// set file pointers
FILE *dialPtr;
FILE *smartPtr;
//input tab file name
char myFile[32];
//input file to convert
printf("Enter the file to convert: (dialer.txt).\n> ");
scanf("%s", myFile);
//check if files are open
if ((dialPtr = fopen (myFile, "r")) == NULL)
//if not open
{
printf("Unable to open %32s.\n", myFile);
}
if ((smartPtr = fopen ("smartout.txt", "w")) == NULL)
{
printf("Unable to open smartout.txt\n\n");
}
else{
//process files
char line[4096];
while (fgets(line, sizeof(line), dialPtr) != NULL)
{
char *rec;
char *clNum;
char *clName;
char *acct;
char *dbtr;
char *addy;
char *phone;
char *pt;
char *assign;
char *stat;
char *desk;
char *tot;
char *lstPay;
char *lstWork;
char *state=line;//buffer
//extract info from buffer
rec = "REC00004A1";
clNum = strtok(state, "\t");
clName = strtok(state, "\t");
acct = strtok(state, "\t");
dbtr = strtok(state, "\t");
addy = strtok(state, "\t");
phone = strtok(state, "\t");
pt = strtok(state, "\t");
assign = strtok(state, "\t");
stat = strtok(state, "\t");
desk = strtok(state, "\t");
tot = strtok(state, "\t");
lstPay = strtok(state, "\t");
lstWork = strtok(state, "\t\r\n");
//send to file
fprintf(smartPtr, "%-10s%-10s%-30s%-10s%-10s%-30s%-25s%-10s%-30s%-6s%3s%-5s%5s%-8s%14s%-6s\n", rec, clNum, clName, phone, acct, dbtr, addy, phone, pt, assign, stat, desk, tot, tot, lstPay, lstWork);
}
//close files
fclose(smartPtr);
fclose(dialPtr);
}
//success
return 0;
}