Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
passing pointers? Posted by jaqueza on 3 Jun 2007 at 2:12 PM
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;
}

Report
Re: passing pointers? Posted by stober on 3 Jun 2007 at 3:27 PM
: 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)
Report
Re: passing pointers? Posted by jaqueza on 3 Jun 2007 at 4:48 PM
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
this.
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
:
: 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)


Report
Re: passing pointers? Posted by stober on 4 Jun 2007 at 4:08 AM
: 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
: this.
: 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
: :
: : 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)
:
:
:


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)
Report
Re: passing pointers? Posted by jaqueza on 4 Jun 2007 at 10:04 AM
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\n", var, data);*/
sprintf(TempData, "%s = %s\n", 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)





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.