FatFS problem with f_write

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello guys,

This is my code:

char text1[] = "\nhello\n testtesttesttesttesttesttesttesttesttesttesttesttest\n";
	char Number = 250;
	f_open(&Test, "random.txt", FA_CREATE_ALWAYS | FA_WRITE);	
	f_write(&Test, text1,sizeof(text1) , &bw);
	f_write(&Test, "\n",1 , &bw);
	
f_write(&Test, Number, 8, &bw);
	f_close(&Test);

and the result is in the picture. :(

What is messing up the file?
I just want: Text and then write a number to the file, nothing more.
Do you have any ideas?
Thank you

p.s. read the file with notepad++. With windowseditor, neither NUL, nor the breaks are displayed.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"\n" has a length of 2 characters:

f_write(&Test, "\n",sizeof("\n"), &bw);

Also, 'char Number' is being assigned an int. Make it '250' instead of 250.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello thank youok, i thought as a control-character "\n" is one Byte wide.
But why was the text repeated? How is this possible?
As the next step, i wanted to write randomly generated numbers in the txt-file.
But in this case '250' won't work i think.
And why is char assigned as int?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I also think that '\n' is just one byte. Regarding your problem - do you get any warnings when you compile your program? Do not ignore them! What compiler do you use? Most compilers have an option to treat warnings as errors. Select that option and use it everywhere.

Eugene

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Its so hard for me to take a guy seriously that uses a handle. Drop the handle and use your name. What are you afraid of? Gaining the respect of your programming peers? Is there a Mrs Bate? Does she approve of your handle? Is there a subliminal message there for us to understand? A soul crying out in the wilderness? Name a famous book by J. D. Salinger.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see.. you actually want it to be a character, one that isn't ASCII. What datatype does f_write expect?

The text is repeated because it is still in the buffer at &Text. You specified a length of 8, but only wrote something with a length of 1 (and I think it's an invalid character, too). 7 characters were then printed. Here is what the buffer &Text looks like:

\nhello\n testtesttesttesttesttesttesttesttesttesttesttesttest\n\0
\n\0ello\n testtesttesttesttesttesttesttesttesttesttesttesttest\n\0
*\0ello\n testtesttesttesttesttesttesttesttesttesttesttesttest\n\0

Where '*' is character 250 (doesn't exist, afaik).

And while '\n' is indeed 1 byte, "\n" is really ['\n','\0'] - 2 bytes. Looking at the behaviour, though, it seems the null can be ignored, as long as its size isn't used to modify (malloc) &Test at runtime...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@bob
My real name is Johannes. But as i like to play with words, i have many nicknames, that are a little uncommon. i.e. Otto Matic is my pseudonym elswhere.
I'm afraid of nothing and i think, that the real name is not that important in a forum. That's why in the registration-form you mostly get asked for a 'nickname'. The only book from Salinger, i know so far, is 'The Catcher in the Rye'

@Lucas
Ok, the problem might be, that f_write requires a pointer to the data to be written.
I gonna check this tomorrow.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah. The Salinger character in that book used to masturbate. I was hoping you didn't have some clinical psychological syndrome.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In the old days if you wanted to write a single character to a file you would use fputc() rather than fwrite(). Wonder if FatFs has an equivalent? (hint: I cheated and I already know the answer to this question ;-))

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, but with f_putc, do i have to set the character in " " or ' ' or without both?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

" " delimits a string - this is one or more characters in an array in memory with the last location always filled with a 0

' ' delimits a single character.

fputc('A', &fil);

should write the single character 'A' to the file.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

doesn't work:

	int i;
	char text1[] = "\nhello\n test\n";
	char Number = 250;
		
	f_open(&Test, "random.txt", FA_CREATE_ALWAYS | FA_WRITE);
	f_write(&Test, text1,sizeof(text1) , &bw);
	f_putc('\n', &Test);
	
f_write(&Test, &Number, 8, &bw);	
	f_close(&Test);

What's wrong with my address of Number?
What did tlucas mean with his first post, that char is assigned as int?

Proglem II: Using f_putc the compiler tells me: main.c:157: undefined reference to `f_putc'

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When I saw "char Number", I assumed it was for an actual character, not an 8-bit signed number. "=250" puts 0xFA (the character '·', from the extended ASCII chart) in "Number" - the name of the variable and the raw input (not "='·'") points to its use as an actual number, and not a string/char array. I think you mean to do this:

char Number = "250";
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, it's just for testing, because i want to fill the file in this way:

for (i=0;i<200;i++){
		Number = 1 + ( rand() % 200 ) ;			
		f_write(&Test, Number, sizeof(Number), &bw);
	}

I set it to char, because i don't need more than 200 :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But are you wanting to store the raw number
itself or the number converted to text digits?

You seem to be mixing up the two.

The second argument to f_write() is a pointer to the data not a value.
You are passing in the value. The code in f_write() is then interpreting the value as a pointer and attempting to grab data bytes at that RAM location - which I'm betting is definitely not what you are wanting.
Surely this is giving a warning during compile.

The first output looked "funky" because I'm guessing out of happenstance, memory location 250 just happened to be very close to the location where your initial string started.
You told f_write() to write the 8 bytes stored at memory location 250. Which I'm guessing by sheer luck was close the memory location of the original text string.

If you are wanting to print the text digits of the number vs the value of the number, then you will need to convert the number into ascii characters before you write it to the file.

Regarding the count of 8 bytes...
The value of a char will fit in 1 byte.
The conversion of a char from a number to a text string will be 1-3 digits plus potentially a trailing null character.

Time to read up a little bit on the difference between pointers, values, numbers and strings.

You might want to have a look a f_printf()
as I think at expense of some code space it may do what you want all in once easy step.

---- bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Proglem II: Using f_putc the compiler tells me: main.c:157: undefined reference to `f_putc'

Look at ff.h and you find:

#if _USE_STRFUNC
int f_putc (int, FIL*);								/* Put a character to the file */
int f_puts (const char*, FIL*);						/* Put a string to the file */
int f_printf (FIL*, const char*, ...);				/* Put a formatted string to the file */
char* f_gets (char*, int, FIL*);					/* Get a string from the file */
#define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
#define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
#ifndef EOF
#define EOF -1
#endif
#endif

also at the top of the same file:

#define	_USE_STRFUNC	0
/* To enable string functions, set _USE_STRFUNC to 1 or 2. */

f_putc is documented here:

http://elm-chan.org/fsw/ff/en/pu...

which says:

Quote:
_USE_STRFUNC is 1 or 2. When it is set to 2, a '\n' is converted to "\r\n".

So set it to 1 if the text files you are creating will be used in Linux and to 2 if they'll be used in WinDOS