I've sort of got it working. Here's the code I have:
Code:
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Prototypes
char *strConcat(char *str1, char *str2);
// Main entry point
int main(int argc, char *argv[])
{
char *fileData = "";
char **lineData;
int nextChar, lineCount = 0, i;
FILE *file;
if (argc != 2)
{
printf("Usage: %s filename", argv[0]);
return 0;
}
file = fopen(argv[1], "r");
if (file == 0)
{
printf("File not found!");
}
else
{
while((nextChar = fgetc(file)) != EOF)
{
if (nextChar == '\n')
lineCount++;
fileData = strConcat(fileData, (char *)&nextChar);
}
}
fclose(file);
printf("%s\n\n", fileData);
lineData = (char **)malloc(sizeof(char) * lineCount);
lineCount = 1;
lineData = &fileData;
for (i = 0; i < strlen(fileData); i++)
{
if (*(fileData + i) == '\n')
{
*(*lineData + lineCount) = *(fileData + i + 1);
lineCount++;
}
}
for (i = 0; i < lineCount; i++)
{
printf("Line: %i - Char: %c\n", i, *(*lineData + i));
}
getchar();
return 0;
}
char *strConcat(char *str1, char *str2)
{
size_t size = strlen(str1) + strlen(str2) + 1;
char *newStr = (char *)malloc(sizeof(char) * size);
sprintf_s(newStr, size, "%s%s", str1, str2);
return newStr;
}
There seems to be some overwriting in data. When I print out the fileData string at the very end, there's some new characters in it. Here's my test file data.
Code:
_ <
_ $33>100>_ 114^
_<72<101<<108>^
_. /^/<111>\87\>\32\>
&{{{{{{{{{{{{#
, Output: Hello World!