How to write something to txt file in PC

Hello everybody
I'm try to write something to txt file on PC by c language. Because my arduino doesn't have SD card (http://arduino.cc/en/Tutorial/CardInfo), So I want to write some result program in to txt file on my PC .
When I tried to use this code

----------code--------------
// 3-axis Accelerometer
// Sparkfun Electronics Triple Axis Accelerometer Breakout - LIS331
// Arduino UNO

/* Wiring:
UNO LIS331

3.3V VCC
GND GND
10 CS
11 SDA/SDI
12 SA0/SDO
13 SCL/SPC
*/

#include <SPI.h>
#include <stdlib.h>
#include <stdio.h>
//#include <Wire.h>
//#include <ADXL345.h>

//ADXL345 acc;

#define SS 10 // Serial Select -> CS on LIS331
#define MOSI 11 // MasterOutSlaveIn -> SDI
#define MISO 12 // MasterInSlaveOut -> SDO
#define SCK 13 // Serial Clock -> SPC on LIS331

//sensor density
#define SCALE 0.0007324; // approximate scale factor for full range (+/-24g)
// scale factor: +/-24g = 48G range. 2^16 bits. 48/65536 = 0.0007324
//#define SCALE 1; //raw data

#define fp=fopen("E:/test","w");

// global acceleration values
double xAcc, yAcc, zAcc,pitch,Xg,Yg,Zg;

void setup()
{
//acc.begin();
Serial.begin(9600);

// Configure SPI
SPI_SETUP();

// Configure accelerometer
Accelerometer_Setup();
}

void loop()
{
readVal(); // get acc values and put into global variables
//acc.read(&Xg,&Yg,&Zg);
pitch=(atan2(xAcc,sqrt(yAcc*yAcc+zAcc+zAcc))*180.0)/M_PI;
Serial.print(xAcc, 1);
Serial.print(",");
Serial.print(yAcc, 1);
Serial.print(",");
Serial.print(zAcc, 1);
Serial.print(", Pitch=");
Serial.println(pitch,1);

delay(800);

FILE *fp;
fp=fopen("E:/test","w");
fprintf(fp,"This is testing....\n");
fclose(fp);
}

// Read the accelerometer data and put values into global variables
void readVal()
{
byte xAddressByteL = 0x28; // Low Byte of X value (the first data register)
byte readBit = B10000000; // bit 0 (MSB) HIGH means read register
byte incrementBit = B01000000; // bit 1 HIGH means keep incrementing registers
// this allows us to keep reading the data registers by pushing an empty byte
byte dataByte = xAddressByteL | readBit | incrementBit;
byte b0 = 0x0; // an empty byte, to increment to subsequent registers

digitalWrite(SS, LOW); // SS must be LOW to communicate
delay(1);
SPI.transfer(dataByte); // request a read, starting at X low byte
byte xL = SPI.transfer(b0); // get the low byte of X data
byte xH = SPI.transfer(b0); // get the high byte of X data
byte yL = SPI.transfer(b0); // get the low byte of Y data
byte yH = SPI.transfer(b0); // get the high byte of Y data
byte zL = SPI.transfer(b0); // get the low byte of Z data
byte zH = SPI.transfer(b0); // get the high byte of Z data
delay(1);
digitalWrite(SS, HIGH);

// shift the high byte left 8 bits and merge the high and low
int xVal = (xL | (xH << 8));
int yVal = (yL | (yH << 8));
int zVal = (zL | (zH << 8));

// scale the values into G's
// xAcc = xVal;
// yAcc = yVal;
// zAcc = zVal;

xAcc = xVal * SCALE;
yAcc = yVal * SCALE;
zAcc = zVal * SCALE;
}

void SPI_SETUP()
{
pinMode(SS, OUTPUT);

// wake up the SPI bus
SPI.begin();

// This device reads MSB first:
SPI.setBitOrder(MSBFIRST);

/*
SPI.setDataMode()
Mode Clock Polarity (CPOL) Clock Phase (CPHA)
SPI_MODE0 0 0
SPI_MODE1 0 1
SPI_MODE2 1 0
SPI_MODE3 1 1
*/
SPI.setDataMode(SPI_MODE0);

/*
SPI.setClockDivider()
sets SPI clock to a fraction of the system clock
Arduino UNO system clock = 16 MHz
Mode SPI Clock
SPI_CLOCK_DIV2 8 MHz
SPI_CLOCK_DIV4 4 MHz
SPI_CLOCK_DIV8 2 MHz
SPI_CLOCK_DIV16 1 MHz
SPI_CLOCK_DIV32 500 Hz
SPI_CLOCK_DIV64 250 Hz
SPI_CLOCK_DIV128 125 Hz
*/

SPI.setClockDivider(SPI_CLOCK_DIV16); // SPI clock 1000Hz
}

void Accelerometer_Setup()
{
// Set up the accelerometer
// write to Control register 1: address 20h
byte addressByte = 0x20;
/* Bits:
PM2 PM1 PM0 DR1 DR0 Zen Yen Xen
PM2PM1PM0: Power mode (001 = Normal Mode)
DR1DR0: Data rate (00=50Hz, 01=100Hz, 10=400Hz, 11=1000Hz)
Zen, Yen, Xen: Z enable, Y enable, X enable
*/
byte ctrlRegByte = 0x37; // 00111111 : normal mode, 1000Hz, xyz enabled

// Send the data for Control Register 1
digitalWrite(SS, LOW);
delay(1);
SPI.transfer(addressByte);
SPI.transfer(ctrlRegByte);
delay(1);
digitalWrite(SS, HIGH);

delay(100);

// write to Control Register 2: address 21h
addressByte = 0x21;
// This register configures high pass filter
ctrlRegByte = 0x00; // High pass filter off

// Send the data for Control Register 2
digitalWrite(SS, LOW);
delay(1);
SPI.transfer(addressByte);
SPI.transfer(ctrlRegByte);
delay(1);
digitalWrite(SS, HIGH);

delay(100);

// Control Register 3 configures Interrupts
// Since I'm not using Interrupts, I'll leave it alone

// write to Control Register 4: address 23h
addressByte = 0x23;
/* Bits:
BDU BLE FS1 FS0 STsign 0 ST SIM
BDU: Block data update (0=continuous update)
BLE: Big/little endian data (0=accel data LSB at LOW address)
FS1FS0: Full-scale selection (00 = +/-6G, 01 = +/-12G, 11 = +/-24G)
STsign: selft-test sign (default 0=plus)
ST: self-test enable (default 0=disabled)
SIM: SPI mode selection(default 0=4 wire interface, 1=3 wire interface)
*/
ctrlRegByte = 0x30; // 00110000 : 24G (full scale)

// Send the data for Control Register 4
digitalWrite(SS, LOW);
delay(1);
SPI.transfer(addressByte);
SPI.transfer(ctrlRegByte);
delay(1);
digitalWrite(SS, HIGH);
}

---------------------it show error------------------
sketch_jun060657.ino: In function 'void loop()':
sketch_jun060657:69: error: expected unqualified-id before '=' token
sketch_jun060657:69: error: 'fopen' was not declared in this scope
sketch_jun060657:70: error: expected primary-expression before '=' token
sketch_jun060657:70: error: expected primary-expression before '=' token
sketch_jun060657:71: error: expected primary-expression before '=' token
sketch_jun060657:71: error: expected primary-expression before ',' token
sketch_jun060657:71: error: expected ;' before ')' token sketch_jun060657:72: error: expected primary-expression before '=' token sketch_jun060657:72: error: expected primary-expression before ')' token sketch_jun060657:72: error: expected ;' before ')' token

How should i do,please advise me :frowning: :frowning: :frowning:
Thanks.

FILE *fp;
    fp=fopen("E:/test","w");

Your Arduino has an E drive? Really?

You can NOT use code on the Arduino to create a file on the PC. You can use the Arduino to send data to the serial port, where you have an application listening that can write the data to a file.

PaulS:

FILE *fp;

fp=fopen("E:/test","w");



Your Arduino has an E drive? Really?

You can NOT use code on the Arduino to create a file on the PC. You can use the Arduino to send data to the serial port, where you have an application listening that can write the data to a file.

Thanks for your advise.

PaulS:

FILE *fp;

fp=fopen("E:/test","w");



Your Arduino has an E drive? Really?

You can NOT use code on the Arduino to create a file on the PC. You can use the Arduino to send data to the serial port, where you have an application listening that can write the data to a file.

Dear PaulS
I tried to do by your recommend ,so

double byteSent = Serial.write(pitch); //pitch type is double

it error
sketch_jun060657.ino: In function 'void loop()':
sketch_jun060657:69: error: call of overloaded 'write(double&)' is ambiguous
C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:62: note: candidates are: virtual size_t HardwareSerial::write(uint8_t)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:63: note: size_t HardwareSerial::write(long unsigned int)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:64: note: size_t HardwareSerial::write(long int)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:65: note: size_t HardwareSerial::write(unsigned int)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:66: note: size_t HardwareSerial::write(int)

with java i think it will be much easier.

With regards to what PaulS said, here is something that helped me, its not a very clean method, but should work somewhat. Maybe you can refine it.

Things to note:
doesn't work with Serial monitor open as it uses the same port (assuming you're using Uno)
in order to terminate the script you must [ctrl+alt+del] for task manager and end the relevent process

This is not something that you can do, using the only the arduino.

You need to write a program on the pc, that runs on the pc, gets messages from the arduino over the serial connection, and writes the data into a file on the pc hard disk.

This is conceptually not all that difficult, although there seems to be unecessary gratuitous difficulties in getting pc programs to communicate with their own serial ports.

I have found that the language "Processing" works quite well for communicating with the arduino.

champy:
it error

Post the whole sketch (inside code tags).

Writing the data to the serial port is simple enough once you have got over these syntax errors. You will need an application on the PC to read from the serial port and save your data to a file. There are various terminal emulators such as PuTTY and RealTerm which can do that for you. If you're running on Windows you can also use Gobetwino to let the Arduino carry out actions on the PC such as writing to a file - this approach lets the Arduino control the whole process including controlling the file name etc. It would be easy enough to write your own equivalent for other platforms.

sketch_jun060657:69: error: call of overloaded 'write(double&)' is ambiguous

The write() method does not have an overload that takes a float or double. Why are you trying to use write()?

The write() returns the number of characters written. What do you suppose the odds are that the function will successfully write 3.14159 characters to the serial port? There is no excuse for storing that value in a float or double.

Thanks for a lot of your recommend,I will google.

Take a look here:

There is a serious error in this line, almost at the beginning of your sketch,

#define SCALE 0.0007324; // approximate scale factor for full range (+/-24g)

There is a semicolon after the definition, that can cause several errors and very cryptic ones.

maccraft:
There is a serious error in this line, almost at the beginning of your sketch,

#define SCALE 0.0007324; // approximate scale factor for full range (+/-24g)

There is a semicolon after the definition, that can cause several errors and very cryptic ones.

This thread is nearly 5 years old. OP is no longer a member so I doubt he/she will see this message.

While your statement is true about the OP not seeing the reply, posting to an older message that comes up in searches may be beneficial to others finding the message for the first time with a similar problem.

elliott2:
While your statement is true about the OP not seeing the reply, posting to an older message that comes up in searches may be beneficial to others finding the message for the first time with a similar problem.

It's my way of saying "People, be aware that this thread is five years (nearly six now) old". The problem is that not everybody pays attention to the original post's timestamp.