Read and display data from file on screen

Discussion in 'Software' started by Gerrald Toh, Dec 15, 2005.

  1. Gerrald Toh

    Gerrald Toh Private E-2

    This file can enable me to write some stuff into file a.txt.

    How do i write a program to read this file and output "this is my file" to screen.

    thanks

    //write to a file//


    #include <stdio.h>

    int main (void)

    {
    FILE *out;
    out=fopen ("a.txt","w");

    fprintf(out, "%s", "This is my file");

    fclose (out);

    return 0;

    }
     
  2. rmStar-R

    rmStar-R Private E-2

    You just have to change the i/o option of fopen(). See below.
    Code:
    //read a file//
    
    #include <stdio.h>
    
    int main (void)
    {
    FILE *in; //changed name for convinience
    char* str;
    in=fopen ("a.txt","r");
    
    fscanf(in, "%s", str);
    printf("%s", str);
    
    fclose (in);
    
    return 0;
    
    }
    Please check the printf() arguments. I'm quite at lost with c these days. I'm used to cout and cin of c++. hehe.

    Good luck!
     
  3. Gerrald Toh

    Gerrald Toh Private E-2

    char* str;

    what does this mean?

    and what will str become?

    is like int a;
    then a is for integer.

    then str is for?

    Thanks....
    im still very new in this
     
  4. rmStar-R

    rmStar-R Private E-2

    Hey, don't be shy asking questions. We learn by asking.. :)

    The {asterisk} '*' before a variable indicates that the variable is a pointer. You might want to read more of pointers. These are related to arrays using the {square bracket pairs} "[]".

    In this case, you might want to change the char* line with:
    Code:
    char str[100];
    Which would mean that the program could only **hold** up to 100 characters.

    Do you really need to do it in c? The task is relatively easy in c++..
     
  5. Gerrald Toh

    Gerrald Toh Private E-2

    Date Time COD Remark





    Imagine is a 4x4table with the header as above.
    This is the table i need to create. I can draw the lines for the table. As for the headers, i can put the words and certain point on the lcd screen. This should be no problem. my problem is to how to display the correct data at the correct position inside the table.


    i need to retrieve information from a file (eg "a.txt") and printf onto the table.

    eg there is this string of data which i want to insert onto the table
    161205 10:00 2000 this is good

    Date Time COD Remark
    161205 10:00 2000 This is good






    How can i do this?

    //write to a file//


    #include <stdio.h>

    int main (void)

    {
    FILE *out;
    out=fopen ("a.txt","w");

    fprintf(out, "%s", "161205 10:00 2000 this is good ");

    fclose (out);

    return 0;

    }



    When i open the a.txt with notepad, im able to see this data. How can i take out this data and shown in on the table.

    How to read from a file and display out on the table?



    *************************************************************************************************************************\



    let say now i need to show this table of summary.

    Date Time COD Remark
    161205 10:00 2000 This is good
    161205 11:00 3000 ok
    171205 13:00 1000 Not ok




    All this data should be given by the main program and saved in "a.txt", how do i get the data from the file and point them to the exact direction on the tabe.

    In a.txt file, we see lines of data.

    161205 10:00 2000 this is good

    161205 11:00 2000 ok

    171205 13:00 2000 Not Ok

    So how do we take a particular data and printf on the table?

    ......................................................................................................

    ?161205 10:00 2000 this is good (data line)

    Is ? a special character that we can use as pointer?



    Thank you so much.
     
  6. Gerrald Toh

    Gerrald Toh Private E-2

    pardon me for my lack of knowledge. I thought c and c++ are like almost the same?
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds