Write text into a document

07 Sep 2012

Hello,

I want to write formatted text into a document. It works only with numbers, but when I write char's with text, then the compiler write the binary numbers instead of it.

Here's my code:

#include "mbed.h"

LocalFileSystem local("local");
Serial pc(USBTX,USBRX);

int main()
{
    FILE *fp;


    fp = fopen("/local/tabelle.txt", "w");

    if(fp == NULL) {
        pc.printf("Datei konnte nicht geoeffnet werden.\n");
    } else {

        char a[25] = "Zeit";
        char b[25] = "Datum";
        char c[25] = "Startdatum";
        char d[25] = "Enddatum";

        int tabelle[4][4];

        tabelle[0][0] = *a;
        tabelle[0][1] = *b;
        tabelle[0][2] = *c;
        tabelle[0][3] = *d;

        int i, j;

// Loop, Y-Pos
        for(i=0; i<5; i++) {
            // Loop, X-Pos
                for(j=0; j<5; j++) {
                fprintf(fp, "%d ", tabelle[i][j]);
                }

            fprintf(fp, "\n");
        }
    }
    pc.printf("Done.\n");
    fclose(fp);
}
07 Sep 2012

Hello, can this happen because You use %d in fprintf call?

So can change to

fprintf(fp, "%s ", tabelle[i][j]);

help to print it out as character stream?

07 Sep 2012

I use %s ...but then my mbed crashed o.O' ... it works when I use %d, ... strange ... I don't know ...what I can do?... :/

07 Sep 2012

Try *tabelle[i][j] in the fprintf and use a & instead of a * earlier... That would set tabelle to be pointers to the strings and set the fprintf to recieve the string

07 Sep 2012

Can you write it down, I don't know how I can write it.

Giles Barton-Owen wrote:

Try *tabelle[i][j] in the fprintf and use a & instead of a * earlier... That would set tabelle to be pointers to the strings and set the fprintf to recieve the string

07 Sep 2012

#include "mbed.h"
 
LocalFileSystem local("local");
Serial pc(USBTX,USBRX);
 
int main()
{
    FILE *fp;
 
 
    fp = fopen("/local/tabelle.txt", "w");
 
    if(fp == NULL) {
        pc.printf("Datei konnte nicht geoeffnet werden.\n");
    } else {
 
        char a[25] = "Zeit";
        char b[25] = "Datum";
        char c[25] = "Startdatum";
        char d[25] = "Enddatum";
 
        int tabelle[4][4];
 
        tabelle[0][0] = &a;
        tabelle[0][1] = &b;
        tabelle[0][2] = &c;
        tabelle[0][3] = &d;
 
        int i, j;
 
// Loop, Y-Pos
        for(i=0; i<5; i++) {
            // Loop, X-Pos
                for(j=0; j<5; j++) {
                fprintf(fp, "%s ", *tabelle[i][j]);
                }
 
            fprintf(fp, "\n");
        }
    }
    pc.printf("Done.\n");
    fclose(fp);
}

It may need the table declaration to be char* rather than int, however that might work

07 Sep 2012

It doesn't work. :(

The Compiler said: "a value of type "char (*)[25]" cannot be assigned to an entity of type "int"" in file "/main.cpp", Line: 24, Col: 22 "a value of type "char (*)[25]" cannot be assigned to an entity of type "int"" in file "/main.cpp", Line: 25, Col: 22 "a value of type "char (*)[25]" cannot be assigned to an entity of type "int"" in file "/main.cpp", Line: 26, Col: 22 "a value of type "char (*)[25]" cannot be assigned to an entity of type "int"" in file "/main.cpp", Line: 27, Col: 22 "operand of "*" must be a pointer" in file "/main.cpp", Line: 35, Col: 36 "Unable to download. Fix the reported errors..." in file "/"

Giles Barton-Owen wrote:

#include "mbed.h"
 
LocalFileSystem local("local");
Serial pc(USBTX,USBRX);
 
int main()
{
    FILE *fp;
 
 
    fp = fopen("/local/tabelle.txt", "w");
 
    if(fp == NULL) {
        pc.printf("Datei konnte nicht geoeffnet werden.\n");
    } else {
 
        char a[25] = "Zeit";
        char b[25] = "Datum";
        char c[25] = "Startdatum";
        char d[25] = "Enddatum";
 
        int tabelle[4][4];
 
        tabelle[0][0] = &a;
        tabelle[0][1] = &b;
        tabelle[0][2] = &c;
        tabelle[0][3] = &d;
 
        int i, j;
 
// Loop, Y-Pos
        for(i=0; i<5; i++) {
            // Loop, X-Pos
                for(j=0; j<5; j++) {
                fprintf(fp, "%s ", *tabelle[i][j]);
                }
 
            fprintf(fp, "\n");
        }
    }
    pc.printf("Done.\n");
    fclose(fp);
}

It may need the table declaration to be char* rather than int, however that might work

07 Sep 2012

Hello , i made some tests and went out with this.

#include "mbed.h"

LocalFileSystem local("local");
Serial pc(USBTX,USBRX);

int main()
{
    FILE *fp;


    fp = fopen("/local/tabelle.txt", "w");

    if(fp == NULL) {
        pc.printf("Datei konnte nicht geoeffnet werden.\n");
    } else {

        char *a = "Zeit";
        char *b = "Datum";
        char *c = "Startdatum";
        char *d = "Enddatum";
       
        char * tabelle[4][4];

        tabelle[0][0] = a;
        tabelle[0][1] = b;
        tabelle[0][2] = c;
        tabelle[0][3] = d;

        int i, j;

// Loop, Y-Pos
        for(i=0; i<5; i++) {
            // Loop, X-Pos
                for(j=0; j<5; j++) {
                fprintf(fp, "%s ", tabelle[i][j]);
                }

            fprintf(fp, "\n");
        }
    }
    pc.printf("Done.\n");
    fclose(fp);
}

Strings are printed to file but there is some "garbage" because tabelle is 2 dimensional array and only 4 ellements are inicialized so I assume that in other ellements are random data which are in memmory. So there will be needed initialization of other ellements of tabelle array or use 1 dimensional array like tabelle[4] instead of tabelle[4][4].

07 Sep 2012

This is what I meant in last post only 1 dimensional array.

#include "mbed.h"

LocalFileSystem local("local");
Serial pc(USBTX,USBRX);

int main()
{
    FILE *fp;


    fp = fopen("/local/tabelle.txt", "w");

    if(fp == NULL) {
        pc.printf("Datei konnte nicht geoeffnet werden.\n");
    } else {

        char *a = "Zeit";
        char *b = "Datum";
        char *c = "Startdatum";
        char *d = "Enddatum";

        //char * tabelle[4][4];
        char * tabelle[4];

        tabelle[0] = a;
        tabelle[1] = b;
        tabelle[2] = c;
        tabelle[3] = d;

        int i, j;

// Loop, Y-Pos
        for(i=0; i<5; i++) {
            // Loop, X-Pos
            //for(j=0; j<5; j++) {
            fprintf(fp, "%s ", tabelle[i]/*[j]*/);
        }

        fprintf(fp, "\n");
    }
    //}
    pc.printf("Done.\n");
    fclose(fp);
}

But I am not sure if this match Your needs so it seems to be better use code I posted above and inicialize all ellements or print to file only required ellements to not get printed random data from memory.

I am sorry of my bad english because I am from Czech Republic and english is not my origin language.

07 Sep 2012

Ohh, thank you it works :)

The "garbage" is gone, because of this: ;)

char * tabelle[4][4] = {0};

07 Sep 2012

No Problem... I'm from Germany, and my english grammar is bad too ;)

07 Sep 2012

I am happy that works for You.

char * tabelle[4][4] = {0}; Yes, very clever.

If You are from Germany, so we are neighbours:)