I'm going through the K&R examples, and I've run into a snag on 1-17.
My current code:
Code:
#include <stdio.h>
#include <string.h>
const int MAX_ARRAY_SIZE = 1000;
int
main(void)
{
char longLines[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE];
char currentLine[MAX_ARRAY_SIZE];
int linesMaxIndex = 0;
int currentChar;
int currentIndex = 0;
while((currentChar = getchar()) != EOF)
{
if((char)currentChar == '\n')
{
if(currentIndex >= 59)
{
longLines[linesMaxIndex] = strcpy(longLines[linesMaxIndex],
currentLine);
linesMaxIndex++;
}
currentLine = NULL;
currentIndex = 0;
}
currentLine[currentIndex] = (char)currentChar;
currentIndex++;
}
for(int i = 0; i <= linesMaxIndex; i++)
{
printf("%s\n", longLines[i]);
}
return 0;
}
The goal is to pipe a file to stdin and print out any line with 60 (actually, I'm reading the text now and it's 80, but that's a minor detail) characters. I get a compile error on line 21 where I try to copy a string into an array of strings. Can someone give me pointers on the correct way to do this in C?