Pages: [1]   Go Down
Author Topic: Can someone explain to me what this part in the SDFatLib example means?  (Read 1377 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I under stand most of this code including the cout << which is the old way to print stuff out in C++. I am confuse about this part

Code:
   for (uint8_t j = 0; j < 100; j++) {
      // use int() so byte will print as decimal number
      sdout << "line " << int(j) << " of pass " << int(i);
      sdout << " millis = " << millis() << endl;
    }
From SDFatLibV2 example append

What is uint8_t and why not use int?
Also why did they use sdout << instead of sdout.print?

Is this just a way to of coding?

Next post has the full code.
« Last Edit: March 24, 2011, 03:12:01 pm by Temo » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
/*
 * Append Example
 *
 * This sketch shows how to use open for append.
 * The sketch will append 100 line each time it opens the file.
 * The sketch will open and close the file 100 times.
 */
#include <SdFat.h>

// file system object
SdFat sd;

// create Serial stream
ArduinoOutStream cout(Serial);

// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//------------------------------------------------------------------------------
void setup() {
  // filename for this example
  char filename[] = "APPEND.TXT";
 
  Serial.begin(9600);
 
  // pstr() stores strings in flash to save RAM
  cout << endl << pstr("Type any character to start\n");
  while (!Serial.available());

  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!sd.init(SPI_HALF_SPEED)) sd.initErrorHalt();

  cout << pstr("Appending to: ") << filename;

  for (uint8_t i = 0; i < 100; i++) {
    // open stream for append
    ofstream sdout(filename, ios::out | ios::app);
    if (!sdout) error("open failed");
   
    // append 100 lines to the file
    for (uint8_t j = 0; j < 100; j++) {
      // use int() so byte will print as decimal number
      sdout << "line " << int(j) << " of pass " << int(i);
      sdout << " millis = " << millis() << endl;
    }
    // close the stream
    sdout.close();
   
    if (!sdout) error("append data failed");
   
    // output progress indicator
    if (i % 25 == 0) cout << endl;
    cout << '.';
  }
  cout << endl << "Done" << endl;
}
//------------------------------------------------------------------------------
void loop() {}
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 593
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

uint8_t is unsigned 8 bit integer, unsigned means no negative numbers, 8 bits saves some space.
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdint.html

<< is probably an operator overload for sdout.print
Logged

Freelance engineer, consultant, contractor. Graduated from UW in 2013.

Espoo, Finland
Offline Offline
God Member
*****
Karma: 7
Posts: 586
"Oops, try again..."
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see "cout" and "endl", but I don't find any references for them.
Are they part of the SD-library, or native C?
Search gave me only codes where they are included, but I can't find specific informations for these?

Any tips? Thanks!

Cheers,
Kari
Logged


The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

0
Offline Offline
God Member
*****
Karma: 0
Posts: 593
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

cout and endl are a part of std namespace, which is a part of <iostream> , which is sort of standard in C++
Logged

Freelance engineer, consultant, contractor. Graduated from UW in 2013.

Espoo, Finland
Offline Offline
God Member
*****
Karma: 7
Posts: 586
"Oops, try again..."
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I need to find more information about that then!
 smiley-neutral

Cheers,
Kari

EDIT. http://en.wikipedia.org/wiki/Iostream
Logged


The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

Pages: [1]   Go Up
Jump to: