passing pointers? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

passing pointers?

jaquezajaqueza Posts: 14Member
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.

[code]
#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) =='
')//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;
}
[/code]

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    : 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.
    :
    : [code]:
    : #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) =='
    ')//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;
    : }
    : [/code]:
    :


    1. FIL is not the correct spelling -- is spelled FILE.

    2. in main() you should declare it as a pointer
    [code]FILE* Filetxt[/code]

    3. there is no such function as f_open() -- its fopen() and the second parameter is a string not an integer.
    [code]Filetxt = fopen("test.txt", "r"); [/code]

    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
    [code]
    if(n > 0)
    {
    // blabla
    }
    [/code]




    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
  • jaquezajaqueza Posts: 14Member
    Please pardon me, I should have made it clear.
    As I mentioned I wasn't using the standard File System,
    maybe I should have said API or something. I'm actually working on
    [link=http://elm-chan.org/fsw/ff/00index_e.html]this[/link].
    Thanks for the tip on the goto's.


    : 1. FIL is not the correct spelling -- is spelled FILE.
    :
    : 2. in main() you should declare it as a pointer
    : [code]: FILE* Filetxt[/code]:
    :
    : 3. there is no such function as f_open() -- its fopen() and the
    : second parameter is a string not an integer.
    : [code]: Filetxt = fopen("test.txt", "r"); [/code]:
    :
    : 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
    : [code]:
    : if(n > 0)
    : {
    : // blabla
    : }
    : [/code]:
    :
    :
    :
    :
    : =============================================
    : never lie -- the government doesn't like the competition. (Author
    : unknown)


  • stoberstober Posts: 9,765Member ✭✭✭
    : Please pardon me, I should have made it clear.
    : As I mentioned I wasn't using the standard File System,
    : maybe I should have said API or something. I'm actually working on
    : [link=http://elm-chan.org/fsw/ff/00index_e.html]this[/link].
    : Thanks for the tip on the goto's.
    :
    :
    : : 1. FIL is not the correct spelling -- is spelled FILE.
    : :
    : : 2. in main() you should declare it as a pointer
    : : [code]: : FILE* Filetxt[/code]: :
    : :
    : : 3. there is no such function as f_open() -- its fopen() and the
    : : second parameter is a string not an integer.
    : : [code]: : Filetxt = fopen("test.txt", "r"); [/code]: :
    : :
    : : 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
    : : [code]: :
    : : if(n > 0)
    : : {
    : : // blabla
    : : }
    : : [/code]: :
    : :
    : :
    : :
    : :
    : : =============================================
    : : never lie -- the government doesn't like the competition. (Author
    : : unknown)
    :
    :
    :


    Ok -- so all but my comment #5 are not valid. It looks like you are passing all the pointers correctly. Since I don't have the hardware or API functions its impossible for me to compile and test your program.
    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
  • jaquezajaqueza Posts: 14Member
    thanks,

    What I'm actually trying to do is read/write
    and .ini file, I'm trying to adapt a code that
    I have found, it's the smallest and it seems simple
    enough for me to understand, (so I thought), but
    having 'bit of hard time since the original code
    was designed for PC, and there are snippets I don't
    understand.

    It appear tho, that the part of the code I posted above
    is working as it should, so the trouble lies somewhere
    else.

    I'm learning tho, the heavy use of pointers of some of the code
    I found helps make me understand how it's used, well a little bit. :)

    basically all the fprint function is replaced:

    /*fprintf(NewCfgFile, "%s = %s
    ", var, data);*/
    sprintf(TempData, "%s = %s
    ", var, data);
    f_write(&NewCfgFile, TempData, sizeof(TempData), &ByteWritten);



    : Ok -- so all but my comment #5 are not valid. It looks like you are
    : passing all the pointers correctly. Since I don't have the hardware
    : or API functions its impossible for me to compile and test your
    : program.
    : =============================================
    : never lie -- the government doesn't like the competition. (Author
    : unknown)


Sign In or Register to comment.