How to write files more faster in C

This is a discussion on How to write files more faster in C within the C Programming forums, part of the General Programming Boards category; [QUOTE=robwhit;704316] >But it would make the function cleaner. I won't need to look at the function again. >but suppose that ...

  1. #61
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    [QUOTE=robwhit;704316]
    >But it would make the function cleaner.

    I won't need to look at the function again.

    >but suppose that I wanted to make 5 files that had that content? How would I do that? the function would exit every time.

    Just uses a different file name.

    >And what error would the compiler not be able to catch with a snippet that small? Would you really not be able to see it with just a casual glance over the code?

    Don't understand what you are saying.

    >Fast access from what to what?since the buffer is in memory, the only way that you would need disk I/O to access the buffer is if the buffer was in swap space, which would be more likely with a larger buffer.

    Fast access to the data which was in the data file.
    If you have not already read in the whole file you may require further disk reads when
    you want to access a different apart of the file. That is very slow.
    The quickest way is to read the whole file in at the start then no further disk IO
    should be needed.

    >The time it takes to increment a counter is microscopic compared to the time it takes for disk i/o.I think that if a function does one thing, it's simpler than if it does two.

    It's stil time and the poinit is I don't need to write the code for the counter, which takes time and makes the code longer.

    The function may be more complicated but you only write it once and then you can forget
    about it. However you still have to put the error handling into the main program which
    makes the main program look more compliceted.

    When you have put the function into a hundred different programs you will be wishing you had put the error handling in the function, otherwise you will have to write the error handling a hundred times. You will probably make a mistake writing it on a few occasions
    too which will consume even more time.

    Sometimes you might forget to even write the error handling so your program will
    fail and you won't know why.
    That won't be a lot of fun, you could spend hours debugging it.
    I only need to get it right once. You have to get it right every time.
    You might forget the error code, was it 1 or -1 which was an error?
    Who knows? I don't need to worry about that.

  2. #62
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by esbo View Post
    It all needs to be in memory because fast access is required.
    It would be very slow if disk I/O needed to take place to access a part of the
    buffer.
    It also makes the code much simpler. The entire buffer is read or written in one line of code.
    No loop required. Hence no loop counter. It is smple and efficient.
    Hey, you know what else you could do... You could spawn thousands of threads and bump up the thead priorities to real-time. That way you can not only hog all the memory, but also all the CPU! Can you imagine how many thank you letters your customers would be sending you then?

  3. #63
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by esbo View Post
    Passing around 4 gigabytes of data within a program would be bordering on insanity.
    You seem to have a fundamental misunderstanding here. When you pass an array to a function, only the pointer to the first element is passed. Those 4 GB will not be copied or anything, the function will be just told where it resides.

    Currently it seems that your idea is to copy and paste as many instances of the functions (with altered names) as you have files and arrays. If you want to use it in another program you'd have to copy and paste the implementation, go into the function and edit certain things.

    But if it occurred to you to use function parameters, you'd never have to edit the function. Simply call it with different arguments.

    Code:
    int data_save(const char* filename, const int* data, int size)
    {
        ...
        if (fileptr) {
            //save
            return 1; //TRUE
        }
        else {
            return 0; //FALSE
        }
    }
    When you have put the function into a hundred different programs you will be wishing you had put the error handling in the function, otherwise you will have to write the error handling a hundred times.
    If exiting the program from a hundred different places where file I/O might fail is the desired behaviour in many cases, you can write a small and simple function that calls data_save and does error handling.

    Code:
    void save_or_die(const char* filename, const int* data, int size)
    {
        if (!data_save(filename, data, size)) {
            puts("\nCant creat datafile");
            exit(2);
        }
    }
    Now you'll have the best of the two worlds. You can choose whether this behaviour will be used for failures, or you can handle the failure differently if you, for example, chose to continue the program.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #64
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by anon View Post
    You seem to have a fundamental misunderstanding here. When you pass an array to a function, only the pointer to the first element is passed. Those 4 GB will not be copied or anything, the function will be just told where it resides.

    I was thinking more of local variables.
    Anyway I don't as a rule use local arrays or pass array pointers to functions so i don't
    need to worry about how such thing work - I don't need to, wasted energy as far as
    I am concerned.

  5. #65
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by esbo View Post
    I was thinking more of local variables.
    Anyway I don't as a rule use local arrays or pass array pointers to functions so i don't
    need to worry about how such thing work - I don't need to, wasted energy as far as
    I am concerned.
    If you do not want to waste the energy on learning good coding practice - stop wasting your energy on teaching bad practices
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #66
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Hey guys!
    I think esbo has a point. He may not be right in all aspects, but for his own purpose and even in the example shown here.
    I think everybody is too picky about his approach!

    esbo, which OS do you use?

  7. #67
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by manav View Post
    Hey guys!
    I think esbo has a point.
    That's our main problem. Somebody with lack of real programming expirience can think that esbo could have a point.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #68
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Esbo
    Anyway I don't as a rule use local arrays or pass array pointers to functions so i don't
    need to worry about how such thing work - I don't need to, wasted energy as far as
    I am concerned.
    You've simply tipped over, on this point.

    When you have an array of data you're working with in one function, and now you need to do something quite different with that same array of data in another function, you *should definitely* be passing the array to the second function, by using a pointer.

    That's one of the real strengths of C in general, and pointers in particular.

  9. #69
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Well.
    I have read most of the esbo's posts. They are really funny!
    I love you esbo!!!
    esbo can we chat?
    Last edited by manav; 01-08-2008 at 06:14 AM.

  10. #70
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by esbo View Post
    >But it would make the function cleaner.

    I won't need to look at the function again.

    >but suppose that I wanted to make 5 files that had that content? How would I do that? the function would exit every time.

    Just uses a different file name.
    But the file name is hardcoded, so you would need to look at the function again.
    >And what error would the compiler not be able to catch with a snippet that small? Would you really not be able to see it with just a casual glance over the code?

    Don't understand what you are saying.
    I'm saying that even if a mistake did happen, it would be trivial to fix it.
    Sometimes you might forget to even write the error handling so your program will
    fail and you won't know why.
    That won't be a lot of fun, you could spend hours debugging it.
    I only need to get it right once. You have to get it right every time.
    You might forget the error code, was it 1 or -1 which was an error?
    Who knows? I don't need to worry about that.
    What if you used that return code for something else in your program? Then how would you know where the error was?

    I'd be willing to chalk up the other points to a difference in opinion about how to utilize resources.

  11. #71
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by esbo View Post
    I don't want it to be local because there is no benefit in making global data local.
    And that is what makes you a newbie. And what makes you a moron is that you never learn or listen.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #72
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Elysia View Post
    And that is what makes you a newbie. And what makes you a moron is that you never learn or listen.
    I have plently of experince of programming much more that you I expect.
    And that is paid experiece.
    Will leave it to others to judge who is the moron.

  13. #73
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    And there are plenty others here who has years of experience and even jobs with programming. And yet they all tell that you are spreading misinformation.
    I don't who know... who am I believe - they or you with your consistent stupid examples to allocate 4 GB of memory, always use global variables, never specify a return type, blames the OS for shortcomings that Microsoft isn't responsible for, sneeze at what everyone is trying to tell you...

    Hmm, not a tough choice, is it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #74
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I have plently of experince of programming much more that you I expect.
    >And that is paid experiece.
    An experienced newbie is generally called a n00b. If you have enough experience to know better, but still make poor decisions and believe that they're not poor, you fit that description. However, I won't bother trying to correct your thinking because it's obvious you don't listen to reason.

    That's not to say that I always disagree with you completely, but a lot of your posts strike me as trolling because they're so ludicrous.
    My best code is written with the delete key.

  15. #75
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Elysia View Post
    And there are plenty others here who has years of experience and even jobs with programming. And yet they all tell that you are spreading misinformation.
    I don't who know... who am I believe - they or you with your consistent stupid examples to allocate 4 GB of memory, always use global variables, never specify a return type, blames the OS for shortcomings that Microsoft isn't responsible for, sneeze at what everyone is trying to tell you...

    Hmm, not a tough choice, is it?
    It's no good telling me anything. You have to prove it.
    Untill you can do that you are just another pompous windbag mouthing off about how
    much you know, and how important and how big and clever you are. Simple as.
    No proof no belief. End of.

Page 5 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read / write binary files
    By Dark_Phoenix in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2009, 07:56 AM
  2. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21