Thread: Help with a simple program

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    8

    Help with a simple program

    Hi everyone, I'm taking a very basic C programming course, but I'm not very good at it. I was wondering if anyone could help me out with my assignment:

    I have to give a file containing some data, write a program that reads the data from the file and prints it on the screen as it is, along with the line numbers.

    Sample
    Assume that the file test.txt contains the following data
    Hello how are you?
    ac39232

    Enter the name of the file: test.txt

    1. Hello how are you?
    2. ac39232

    Any ideas?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well, since it's an assignment, we can't give much information. Study your File I/O. As for the line number, I can give you a hint.

    There is a special character at the end of each line in the file. When you hit that character, it means you're starting a new line in the file. Do you know what it is? Read in a string until you encounter this character, output the current line number, the line you just read, and then increment the line counter.
    Sent from my iPadŽ

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or better yet, try fgets().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    First, we'd like to see you try. Since it's an assignment, you should have an idea from class on how to go about it. Just post something you've done; doesn't matter if it doesn't work. The important thing is that you tried.

    If you weren't paying attention in class and expect us to do it for you, forget it.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Agreed - start a new thread with your attempt.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM