Thread: Trying to print a data file with words and numbers

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Trying to print a data file with words and numbers

    Hi, I'm trying to create a menu for a program by printing a data file. This is what it looks like

    WELCOME TO MAZE-RUNNER DEMO


    THIS IS NOT YOUR MAZE RUNNER! This is a demo of possible Maze Runner Levels.

    The Levels are:

    Level 0: Drive the Mouse.
    You may use the a,s,d,w commands in the predefined
    visible maze.

    Level 1: Mouse Discovers
    You may use Level 0 commands and r,l and m commnad
    in the predefined hidden maze

    Level 2: Mouse Explores
    You may use Level 1 commands and the n command

    Level 3: Da Frills
    Not provided.


    and i'm trying to print it like this

    insert
    Code:
    #include<stdio.h>
    #include "menu.h"
    
    main()
    {
           int lvl = 100;
            char f;
            FILE *fp;
            char words [100];
    
    
    
           fp = fopen ("demo_intro", "r");
    
            fscanf(fp, "%s", &f);
            fprintf(fp, "%-100.100s",f);
    }
    I have never used fprint,fscan, or fopen before so I don't know how to use them at all. I am hoping someone will first be able to tell me if i can actually print that data file and secondly, teach me how to open and print it using code. Thank you a lot.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You should then look up the tutorials of this or any other site first!
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    2
    I did look it up, that's how i got that code, but i don't understand why it won't work because it gives me segmentation fault.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    First, you don't check that you successfully opened the file. Second,
    Code:
    char f;
    ...
    fscanf(fp, "%s", &f);
    you're attempting to use the character string formatter, %s, to read a single character.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That's perhaps because you are trying to read a string into a character. Also main() should be int main(void) and should return 0.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Something like this:
    Code:
    fgets(buffer, BUF_SIZE, fh);
    printf("buffer: %s\n", buffer);

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Weyoun92 View Post
    I did look it up, that's how i got that code, but i don't understand why it won't work because it gives me segmentation fault.
    Do you not have documentation for your compiler and it's libraries?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print Hexadecimal Numbers Of a File
    By nathanpc in forum C Programming
    Replies: 4
    Last Post: 01-13-2010, 11:19 PM
  2. Read words and numbers from a file
    By atari400 in forum C Programming
    Replies: 5
    Last Post: 11-04-2004, 04:55 PM
  3. Print binary numbers to disk file, problem
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 10-04-2004, 07:33 AM
  4. Print data in text file out to paper
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 07-24-2002, 08:33 AM