counting number of lines in my output file

This is a discussion on counting number of lines in my output file within the C Programming forums, part of the General Programming Boards category; But consider the following: Code: line1\n line2\n line3 You'd now report there was 2 lines, incorrect -- there's 3....

  1. #16
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    But consider the following:
    Code:
    line1\n
    line2\n
    line3
    You'd now report there was 2 lines, incorrect -- there's 3.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    How about this then:
    Code:
    	FILE* f;
    	int lines = 0;
    	char Buffer[10000];
    	fopen_s(&f, "C:\\test.txt", "r");
    	while ( fgets(Buffer, sizeof(Buffer), f) )
    	{
    		if (Buffer[strlen(Buffer) - 1] == '\n') lines++;
    	}
    	lines++;
    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.
    よく聞くがいい!私は天才だからね! ^_^

  3. #18
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Wrong. You'd report 1 line for an empty file.
    Kurt

  4. #19
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Not to mention you're using sick twisted Microsoft renamed functions.

    fopen_s, ewwwww.

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    Quote Originally Posted by ZuK View Post
    Wrong. You'd report 1 line for an empty file.
    Kurt
    Hmmm. Additional logic is required.

    Quote Originally Posted by zacs7 View Post
    Not to mention you're using sick twisted Microsoft renamed functions.

    fopen_s, ewwwww.
    They're a safe version of the runtime library, which isn't Microsoft specific AFAIK.
    Even if they were, I'd rather use safe functions than unsafe. They're easy to do away with with macros or wrappers anyway.
    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.
    よく聞くがいい!私は天才だからね! ^_^

  6. #21
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Elysia View Post
    They're a safe version of the runtime library, which isn't Microsoft specific AFAIK.
    Even if they were, I'd rather use safe functions than unsafe. They're easy to do away with with macros or wrappers anyway.
    fopen() is unsafe how?

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    Quote Originally Posted by MacGyver View Post
    fopen() is unsafe how?
    Well, I didn't say it's unsafe, but if there's a "safer" version available, I'll use it.
    Besides, fopen is deprecated (in Visual Studio 2005/2008!). That's reason enough to avoid it.
    This code should work better:

    Code:
    bool ReadLine(FILE* f, char* pBuffer, size_t nSize);
    
    void Help()
    {
    	FILE* f;
    	int lines = 0;
    	char Buffer[10000];
    	if (fopen_s(&f, "C:\\test.txt", "r") != 0)
    	/* f = fopen("C:\\test.txt", "r");
    	if (f == NULL) */
    	{
    		// ERROR: Failed to open file.
    		printf("Failed to open file!");
    		return;
    	}
    	if ( ReadLine(f, Buffer, sizeof(Buffer)) )
    	{
    		while ( ReadLine(f, Buffer, sizeof(Buffer)) )
    			lines++;
    		lines += 2;
    	}
    }
    
    int ReadLine(FILE* f, char* pBuffer, size_t nSize)
    {
    	do 
    	{
    		if (fgets(pBuffer, nSize, f) == NULL)
    			return 0;
    	}
    	while (pBuffer[strlen(pBuffer) - 1] != '\n');
    	return 1;
    }
    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.
    よく聞くがいい!私は天才だからね! ^_^

  8. #23
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Elysia View Post
    Besides, fopen is deprecated (in Visual Studio 2005/2008!). That's reason enough to avoid it.


    Oh, yes. If Microsoft says it, it must be true.

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    OK, so let me rephrase:
    There's reason enough to avoid it in Visual Studio, since it will be removed in future versions of Visual Studio.
    And I don't see what's wrong with using fopen_s instead of fopen if fopen_s is a newer function. Even if it isn't more secure, at least it returns the error instead of having you to check doserr or whatever they're called.
    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.
    よく聞くがいい!私は天才だからね! ^_^

  10. #25
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Elysia View Post
    OK, so let me rephrase:
    There's reason enough to avoid it in Visual Studio, since it will be removed in future versions of Visual Studio.
    Wrong again.

    http://msdn2.microsoft.com/en-us/lib...kh(VS.80).aspx

    It should be noted that in this context, "deprecated" just means that a function's use is not recommended; it does not indicate that the function is scheduled to be removed from the CRT.
    Quote Originally Posted by Elysia View Post
    And I don't see what's wrong with using fopen_s instead of fopen if fopen_s is a newer function. Even if it isn't more secure, at least it returns the error instead of having you to check doserr or whatever they're called.
    Portability?

    Besides, how hard is it to check errno?

  11. #26
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    Quote Originally Posted by MacGyver View Post
    OK, so that goes to show that Microsoft is good at confusing people. I agree.

    Portability?

    Besides, how hard is it to check errno?
    As I told you, it's not difficult to do defines or wrappers to "emulate" the safe functions and wrap them to the plain, old functions.
    It's much harder to do it the other way around.
    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. #27
    Registered User Josh@Dreamland's Avatar
    Join Date
    Dec 2007
    Posts
    10
    Where are you getting the list you need to load into memory? OR When are you adding to the list?

    You could create a function that mallocs a new array of the first array's size +1, then copy the old array over right before freeing it.

    You can get the size where size_of_array = (sizeof array)/(sizeof array[0]);

    I have no idea how to write all the integers into the malloc'd space.

  13. #28
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    OK, so let me rephrase:
    There's reason enough to avoid it in Visual Studio, since it will be removed in future versions of Visual Studio.
    And I don't see what's wrong with using fopen_s instead of fopen if fopen_s is a newer function. Even if it isn't more secure, at least it returns the error instead of having you to check doserr or whatever they're called.
    I almost always define _CRT_SECURE_NO_WARNINGS to get rid of those annoying deprecated warnings. For those functions that VC++ broke by renaming them (with a '_' at the front), I just use a #define to restore their proper names.
    I prefer my programs to be compilable (or at least easy to port) on UNIX, even when I don't have any immediate plans to compile it on UNIX.

  14. #29
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    It's a shame not to use safe functions if they're available. Why not use them if you have them? They were designed for a reason - to avoid some very common mistakes.
    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.
    よく聞くがいい!私は天才だからね! ^_^

  15. #30
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because they're not portable, it's Microsoft's ideology that they're the leaders of setting standards (in this case they're trying to peel back the current ones).

    Microsoft 'secure' CRT functions, please don't make me laugh.
    Last edited by zacs7; 12-25-2007 at 05:00 PM.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Counting Number of Lines of file
    By dapernia in forum C Programming
    Replies: 1
    Last Post: 09-05-2003, 02:22 PM

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