Thread: c-style file steam scope

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    c-style file steam scope

    would the code below (granted I didnt type it wrong) create a file and then write to it or create a file, and attempt to write to it although it is "out of scope" per say, created in a separate function, creating an error?

    Code:
    int main()
    {
         file_stream_declaration();
         
         char[15] characters_written = "characters";
         fputs(characters_written, fopen);
    
         return 0;
    
    file_stream_declaration()
    {
         FILE *fopen("temporary_file.txt" "w+");
         return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Arcand
    would the code below (granted I didnt type it wrong) create a file and then write to it or create a file, and attempt to write to it although it is "out of scope" per say, created in a separate function, creating an error?
    Yes. Of course, since you did type it wrong, it won't work.

    I suggest that you actually post the code that you tried and tell us how does it not work.
    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

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Arcand View Post
    would the code below (granted I didnt type it wrong)
    You did type something wrong
    I can't understand what you try to do, and I don't understand your question.
    Kurt
    Last edited by ZuK; 08-22-2012 at 08:40 AM.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
    int main()
    {
         file_stream_declaration();
          
         char[15] characters_written = "characters";
         fputs(characters_written, fopen);
     
         return 0;
     
    file_stream_declaration()
    {
         FILE *fopen("temporary_file.txt" "w+");
         return 0;
    }
    Errors:

    • Your syntax for declaring a char array is wrong
    • You're missing an end brace for main.
    • Your function is returning 0 but the return type is unspecified
    • You can't pass a function to fputs
    • Fopen is a function that returns a FILE *, you're missing a variable name


    Other issues:

    • You need some way to pass the opened file to main, usually you'd use return to return the opened file.
    • You aren't checking to see if the file is actually opened
    • You aren't closing the file when you're done

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It looks to me like this is your first attempt for file I/O - Don't be discouraged, but there is a lot that needs to change

    I think that the best thing that you could do is read through a quick tutorial on how this is done - "C programming - C File I/O tutorial"

    After reading through this tutorial, have another attempt and then if you still are having problems, post your code

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    On a second look at your code, (based on the scope of variables) you are new to the C-language (right?)

    There is more than tutorials on file I/O on this site - C Tutorial - Learn C - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Steam overlay - how?
    By VirtualAce in forum Tech Board
    Replies: 3
    Last Post: 02-28-2011, 07:02 PM
  2. File-scope objects
    By BdON003 in forum C++ Programming
    Replies: 11
    Last Post: 11-23-2009, 04:16 PM
  3. variable in file scope ???
    By howhy in forum C++ Programming
    Replies: 5
    Last Post: 08-30-2005, 04:46 AM
  4. multiple file scope question
    By owi_just in forum C Programming
    Replies: 5
    Last Post: 04-23-2005, 03:38 AM
  5. Steam Help...
    By Captn Japan in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2003, 02:42 AM