: I'm trying to read a line from
: a text, I'm not using standard File System.
: with the code below, am I passing pointers
: the right way calling fgetsf()?
: I seem to be getting garbage in the readline()
: printf.
:
: thanks.
:
:
:
: #define LAST_CHAR(s) (((char *)s)[strlen(s) - 1])
: #define LINESIZE 40;
: WORD ByteRead;
: /*
: ** f_read (
: ** FIL* FileObject, // Pointer to the file object structure //
: ** void* Buffer, // Pointer to the buffer to store read data //
: ** WORD ByteToRead, // Number of bytes to read */
: ** WORD* ByteRead // Pointer to the variable to return number of bytes read //
: ** )
: */
: char * fgetsf(char * s, int n, FIL *fil)
: {
: char c;
: char * p;
:
: if(n <= 0)goto ERROR;
: p = s;
: while (--n) {
: if (f_read(fil, &c , 1 , &ByteRead) == FR_OK){
: if ((*p++ = c) =='\n')//end of line
: break;
: }
: if(ByteRead == 0) //eof
: break;
: }
:
: if (p > s) {
: *p = 0;
: return s;
: }
: ERROR:
:
: return NULL;
: }
:
: /*
: **
: */
: static int ReadLine(FIL *fp, char *line)
: {
: char *cp;
:
: cp = fgetsf(line, LINESIZE, fp);
:
:
: printf(cp);//debug
:
:
: return strlen(line);
: }
:
: /*
: **
: */
: int main(){
:
: FIL Filetxt;// File Object structure
: char line[LINESIZE];
: int linelen;
:
: f_open(&Filetxt, "test.txt", FA_OPEN_ALWAYS | FA_READ);
:
: linelen = ReadLine(&Filetxt, line);
:
: printf("%d",linelen);
:
: return 0;
: }
: :
:
1. FIL is not the correct spelling -- is spelled FILE.
2. in main() you should declare it as a pointer
FILE* Filetxt
3. there is no such function as f_open() -- its fopen() and the second parameter is a string not an integer.
Filetxt = fopen("test.txt", "r");
4. f_read() -- there is no such function, use fread() instead.
5. >>if(n <= 0)goto ERROR;
delete that goto statement -- many, if not most, teachers would (should) give an F for a program that uses goto. Instead you can use this construct
if(n > 0)
{
// blabla
}
=============================================
never lie -- the government doesn't like the competition. (Author unknown)