Go Down

Topic: Can someone explain to me what this part in the SDFatLib example means? (Read 1 time) previous topic - next topic

Temo

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: [Select]
   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.

Temo

Code: [Select]
/*
* 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() {}

frank26080115

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
Freelance engineer, consultant, contractor. Graduated from UW in 2013.

GaryP

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
The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

frank26080115

cout and endl are a part of std namespace, which is a part of <iostream> , which is sort of standard in C++
Freelance engineer, consultant, contractor. Graduated from UW in 2013.

GaryP

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

Go Up