1. Learn to indent code, eg.
Code:
#include<stdio.h>
void main()
{
char temp[10];
FILE *input1 = NULL;
FILE *input2 = NULL;
unsigned char buffer[100], lTempData[7][20];
char *lPtr = NULL;
int i =0;
input1 = fopen("/u/n073313/vijay/File.txt", "rb");
fread(buffer, 1,100, input1);
{
printf("First line %s\n",buffer);
lPtr = strtok(buffer, ",");
strncpy(lTempData[i], lPtr, strlen(lPtr));
while (lPtr != NULL)
{
i++;
strncpy(lTempData[i], lPtr, strlen(lPtr));
}
for (int j =0; j< 7 ; j++)
printf("Values %s\n", lTempData[i]);
int a = atol(lTempData[5]);
int b = atol(lTempData[6]);
int c = a+b;
printf("Values %d %d result %d\n", a, b, c);
sprintf(temp, "%d", c);
input2 = fopen("File2.txt", "r");
fwrite(c, 1, 10, input2);
fclose(input1);
fclose(input2);
}
}
2. main returns int, not void.
3. using fread to read a text file is very poor.
a) it won't stop at a newline
b) it won't have a \0, for the benefit of the following string functions
c) it should have been in a loop, if you want to process the whole file.
Code:
while ( fgets( buffer, sizeof buffer, input1 ) != NULL ) {
// do stuff with each line
}
4. strncpy(lTempData[i], lPtr, strlen(lPtr));
It's nice that you're using strncpy to protect your strings from overflow, but
a) you should use the size of the destination, not the size of the source
b) you also need to ensure a \0 is copied (strncpy doesn't always do this).
5. while (lPtr != NULL)
What changed lPtr in this loop?
If it gets into the loop, it's not getting out anytime soon.