Thread: Question: Output of data.txt from the following program

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    7

    Question: Output of data.txt from the following program

    Hi,

    I need to know what should be in the data.txt file after the program in the attached file will run.

    What the fucntion does ? and why I see in the data.txt file two smiley (one white & one black)

    Thanks,
    E

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You opened a file, but wrote nothing to it:

    return 1;
    fwrite(p,sizeof(int),n+1,fp);

    If you want something else written to the file, put it in front of the return statement. I'm guessing that what you see are what makes up the file header

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Your function func is attempting to read a file in binary format. In order to do that you need to open your dat file in the right mode.

    There are things not to like about your program. For one, func's body is not complete.

    Code:
    while (scanf("%d",&x)!=EOF){
                   func(fp,x);
    }
    return 1;
    fwrite(p,sizeof(int),n+1,fp);
    I think you know that nothing after a return statement will be executed.

    Also, scanf works on files opened in text mode, not binary mode. Are you sure you need to use fread? Until you make up your mind how to open your file and read it, it won't matter what you fix.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    Let's say that the input will be the number 4 8 10 1 4, so what should be in the data.txt file.

    The input is text and the file data.txt will be binary, even that is .txt

    In other words what does the function ?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    If you want to know what is the output, then run the program. If you want to know what the function does, then volunteer your answer first. Who wrote that function, anyway?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Never mind...misread what you were trying to do.
    Last edited by rags_to_riches; 06-23-2008 at 12:05 PM. Reason: Oops

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    Quote Originally Posted by laserlight View Post
    If you want to know what is the output, then run the program. If you want to know what the function does, then volunteer your answer first. Who wrote that function, anyway?


    Sorry it's beacuse my poor english.

    My teacher wrote the function and I want to know what she do.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And she didn't tell you what it does? Could that be because she wants YOU to determine what it does?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by outlaw525 View Post
    Sorry it's beacuse my poor english.

    My teacher wrote the function and I want to know what she do.
    You don't need to know English; you need to know C. Sit down with a piece of paper, decide what your input is going to be (which you already seem to have done), and step through it line-by-line and watch what happens.

    Granted, nothing will get written to the file, as mentioned earlier, since the return 1 comes first. I don't feel like looking it up right this minute, but calling fread on a file pointer opened with "w+" worries me too. But you can pretend all those are fixed and watch what happens.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    I don't feel like looking it up right this minute, but calling fread on a file pointer opened with "w+" worries me too. But you can pretend all those are fixed and watch what happens.
    The "+" makes it technically OK to read and write the same file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program looping with final output
    By hebali in forum C Programming
    Replies: 24
    Last Post: 02-28-2008, 10:58 AM
  2. Shell output into program during runtime
    By evilkillerfiggi in forum Linux Programming
    Replies: 3
    Last Post: 08-03-2006, 10:50 AM
  3. GPL license: how does it cover the output of a program?
    By psychopath in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 05-25-2006, 01:37 AM
  4. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  5. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM