Scusate,
ho questa funzione per visualizzare un file di testo:
codice:
#define CR 13 /* Decimal code of Carriage Return char */
#define LF 10 /* Decimal code of Line Feed char */
#define EOF_MARKER 26 /* Decimal code of DOS end-of-file marker */
#define MAX_REC_LEN 1024 /* Maximum size of input buffer */
#define MAX_NUM_LINE 32
#define MAX_LINE_LENGTH 30
int T_fgetc(FILE *input, char *output){
int iReturn = 1; /* Return value (Innocent until proved guilty) */
int iThisChar; /* Current character */
int isNewline = 0; /* Boolean indicating we've read a CR or LF */
long lIndex = 0L; /* Index into read buffer */
while (1) /* Will exit on error, end of line, or end of file */
{
iThisChar = fgetc(input); /* Read the next character */
if (ferror(input)) /* Error reading */
{
iReturn = -1; /* Convert to negative number */
break;
}
if (iThisChar == EOF) /* End of file reached */
{
if (lIndex > 0)
ungetc(iThisChar, input);
else /* Nothing read but EOF; we're done with the file */
iReturn = 0;
break;
}
if (!isNewline) /* Haven't read a CR or LF yet */
{
if (iThisChar == CR || iThisChar == LF) /* This char IS a CR or LF */
isNewline = 1; /* Set flag */
}
else /* We've already read one or more CRs or LFs */
{
if (iThisChar != CR && iThisChar != LF) /* This char is NOT a CR or LF */
{
ungetc(iThisChar, input); /* Put char back in stream */
break; /* Done reading this line */
}
}
output[lIndex++] = iThisChar; /* Put char in read buffer */
} /* end while (1) */
output[lIndex] = '\0'; /* Terminate the read buffer */
return iReturn;
} /* end T_fgetc() */
void PrintLine(char *szReadLine, long lLineCount, long lLineLen, long lThisFilePos, long *lLastFilePos, int isFilePosErr){
char *cPtr;
cPtr = szReadLine;
for (cPtr = szReadLine; cPtr <= szReadLine + lLineLen; cPtr++)
{
switch (*cPtr)
{
case 0: /* Null terminator */
break;
case CR: /* Carriage return */
break;
case LF: /* Line feed */
printf("\n");
break;
default: /* A 'real' character */
printf("%c", *cPtr);
break;
}
}
printf("\n");
return;
}
int Reader(char* filen, int linestart)
{
int (*GetLine[1])(FILE*, char*) = { T_fgetc };
char tmode[5];
int iReadMode=0; /* Index into *GetLine[] array */
int iReadReturn; /* Result of read function */
int isFilePosErr; /* Boolean indicating file offset error */
long lFileLen; /* Length of file */
long lLastFilePos; /* Byte offset of end of previous line */
long lLineCount; /* Line count accumulator */
long lLineLen; /* Length of current line */
long lThisFilePos; /* Byte offset of start of current line */
char szReadLine[MAX_REC_LEN];
FILE *inputFilePtr; /* Pointer to input file */
strcpy(tmode, "t");
if (strcmp(tmode, "t") == 0)
inputFilePtr = fopen(filen, "r"); /* Open in TEXT mode */
else if (strcmp(tmode, "b") == 0)
inputFilePtr = fopen(filen, "rb"); /* Open in BINARY mode */
if (inputFilePtr == NULL ) /* Could not open file */
{
printf("Error opening %s\n", filen);
return 1;
}
fseek(inputFilePtr, 0L, SEEK_END); /* Position to end of file */
lFileLen = ftell(inputFilePtr); /* Get file length */
rewind(inputFilePtr); /* Back to start of file */
lLineCount = 0L; /* No lines read yet */
lLastFilePos = -1L; /* So first line doesn't show an offset error */
cls();
while (1)
{
isFilePosErr = 0;
lThisFilePos = ftell(inputFilePtr);
if (lThisFilePos != lLastFilePos + 1){
isFilePosErr = 1;
}
szReadLine[0] = '\0';
/* Read the next line with the appropriate read function */
iReadReturn = (*GetLine[iReadMode])(inputFilePtr, szReadLine);
if (iReadReturn < 0)
{
printf("Error reading: %s\n", filen);
break;
}
lLineLen = strlen(szReadLine); /* Get length of line */
if (lLineLen) /* Got some data */
{
++lLineCount; /* Increment line counter */
}
if(lLineCount>=linestart && lLineCount<(linestart+MAX_NUM_LINE)){
PrintLine(szReadLine, lLineCount, lLineLen, lThisFilePos, &lLastFilePos, isFilePosErr);
}
if (iReadReturn == 0) /* End of file reached */
break;
}
fclose(inputFilePtr); /* Close the file */
return 0;
}
Il problema è che devo poter scorrere il testo e non posso usare delle API, con la funzione Reader dovrei passare il nome del file e la linea di partenza per la visualizzazione sullo schermo, ed essa aumenta e diminuisce quando premo rispettivamente giu e su!, solo che quando avvio questa funzione mi viene fuori un bel casino, dato che se la stringa è più lunga della finestra me la manda a capo e la conta come un'altra linea!
Qualcuno può darmi una aiuto?