Hi guys, I've got some code for a C program that splits a text file.
It splits a text file into different components, and currently works for text files with YYYYMMDD.txt format. For example, here are two daily files:
20090101.txt
ACD,20090101,2738.0801,2738.0801,2738.0801,2738.08 01,0
ADR,20090101,626.15,626.15,626.15,626.15,0
ADRA,20090101,2.58,2.58,2.58,2.58,0
20090102.txt
ACD,20090102,2738.08,2799.71,2717.33,2799.64,0
ADR,20090102,626.15,642.47,625.42,640.72,0
ADRA,20090102,0.45,2.42,0.45,2.42,0
What it does is make ACD.txt, ADR.txt and ADRA.txt and append each days data onto the end of each file. For example;
ACD.txt
20090101,2738.0801,2738.0801,2738.0801,2738.0801,0
20090102,2738.08,2799.71,2717.33,2799.64,0
The code I've got only works for files in the YYYYMMDD.txt type format. I want to modify it so that it works for any text format (such as TEST_YYYYMMDD.txt).
If someone could point me into what to change, that would be greatly appreciated. I've studied the code and can't see it.
Code:
#include <stdio.h>
#include <string.h>
#define EXTENSION ".txt"
int main(void)
{
int count=0;
char fileName[100];
char TempfileName[100];
char TempString[256];
char TempChar;
FILE *fp;
FILE *fpTmp;
printf("Enter Filename: ");
scanf("%s", fileName);
printf("Opening %s...\n", fileName);
fp = fopen(fileName, "rb");
if(fp == NULL)
{
fprintf(stderr, "Error Opening File: %s\n", fileName);
return -1;
}
else
printf("%s Opened Successfully.\n\n\n",fileName);
while(!feof(fp))
{
for(count=0; ( (TempfileName[count] = fgetc(fp)) != ',' ) ; count++);
TempfileName[count] = '\0';
strcat(TempfileName,EXTENSION);
fpTmp = fopen(TempfileName,"ab");
if(fpTmp == NULL)
{
fprintf(stderr, "Error Opening File: %s\n", TempfileName);
return -1;
}
else
printf("%s Opened Successfully.\n",TempfileName);
fgets(TempString,256,fp);
TempString[strlen(TempString)-1] = '\n';
fputs(TempString, fpTmp);
printf("%sWritten to %s\n", TempString, TempfileName);
fclose(fpTmp);
printf("%s Closed.\n\n",TempfileName);
}
}
Also, this program extracts the text files into the same directory, how would I go about putting them into a sub directory such as "extracted". e.g. if it was in C:/Data, extract it to C:/Data/Extracted?
Thanks all.