fopen, matching braces, pointers

This is a discussion on fopen, matching braces, pointers within the C Programming forums, part of the General Programming Boards category; Good morning. Can someone please explain why this give me no compiler warnings or errors: Code: for( counter = BEGINNINGOFHTMLLIST; ...

  1. #1
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391

    fopen, matching braces, pointers

    Good morning.

    Can someone please explain why this give me no compiler warnings or errors:

    Code:
    for( counter = BEGINNINGOFHTMLLIST; counter < (lastfilenumber - 1); counter++) 
    	{	
    		printf("Filename #%d: %s\n", counter, LineData[counter].string2);	
    	}
    
        return 0;
    }
    These codes are copied straight from my last post(#10) in this thread:

    how to read strings?

    But when I change it to this(my aim is to open and close every file).

    Code:
    for( counter = BEGINNINGOFHTMLLIST; counter < (lastfilenumber - 1); counter++) 
    	{	
    		if((pOpenfile = fopen(LineData[counter].string2, "rt")) == NULL)
    		{
    			perror("\nError opening file");
    			exit(EXIT_FAILURE);
    		}
    
    		fclose{LineData[counter].string2);  <===(line 75)==
    	}
    
        return 0;
    }   
         <===(line 80)====
    I get the following compiler warnings and errors.

    Code:
    (75) : error C2143: syntax error : missing ';' before '{'
    (75) : warning C4550: expression evaluates to a function which is missing an argument list
    (75) : error C2059: syntax error : ')'
    (80) : fatal error C1075: end of file found before the left brace '{' at (11)' was matched
    I can't find any non-matching braces.

    1. Could the problem be that I am trying to use a pointer to a string in the fopen function, instead of using a literal string ie. "website.html"?

    2. Does anyone know how to set up matching braces in VC++ 2008 Express? I have followed the instructions, but I can't seem to use the feature. How do I check for matching braces?

    3. Say I wanted to use
    Code:
    pOpenfile = fopen("c:\website\bobthebuilder.html", "rt");
    How would I achieve this with a pointer to the string "bobthebuilder.html"?

    Say I had:
    Code:
    LineData[0].string2 = "bobthebuilder.html"
    How could I achieve something like
    Code:
    fopen("c:\website\%s",LineData[0].string2, "rt");
    Thanks in advance.
    Last edited by happyclown; 01-23-2009 at 04:23 PM. Reason: added more content
    OS: Linux Mint 13(Maya) LTS 64 bit.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Code:
    fclose{LineData[counter].string2);

  3. #3
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by kermit View Post
    Code:
    fclose{LineData[counter].string2);
    Sweet mother of God! Thanks kermit.

    EDIT:

    I am now getting "Error opening file: No such file or directory". Is it because I can't use pointers in fopen()?
    Last edited by happyclown; 01-23-2009 at 04:28 PM.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Well, does the file you are trying to open actually exist? You are trying to open it in read only mode, so if it does not exist, the call to fopen will fail.
    Last edited by kermit; 01-23-2009 at 04:50 PM.

  5. #5
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    You're right again, kermit!

    That'll teach me to try to program 30 seconds after waking up in the morning!

    I have 942 files for it to open and close. The program now runs, for about 14 seconds, then crashes and control returns to the command prompt. I can see CPU usage at 100% as soon as I start the program.

    I wonder if there is a way for me to see what it is doing during those 14 seconds? I'll try to figure this one out. And will post back here if I still need help.

    Thanks.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by happyclown View Post
    That'll teach me to try to program 30 seconds after waking up in the morning!
    Yeah, I've done that before too. You think you are awake, but then you realise your brain is still in bed.

  7. #7
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Just as a matter of interest, I opened up the files for writing in an empty directory.

    It created 5 files, when it should have created 942 files, before crashing.

    So it appears the program works up to and including the 5th files. So what the heck is it doing the rest of the time before crashing?

    I will investigate! This should be alot fun.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Shouldn't you be closing the file handle and not the name of the file?

  9. #9
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by tabstop View Post
    Shouldn't you be closing the file handle and not the name of the file?
    Yep, program works great now.

    Thanks for everybody's help.

    EDIT: I posted the working code here, but it kept running off the screen, so I took it off.
    Last edited by happyclown; 01-23-2009 at 10:27 PM.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Quote Originally Posted by happyclown View Post
    2. Does anyone know how to set up matching braces in VC++ 2008 Express? I have followed the instructions, but I can't seem to use the feature. How do I check for matching braces?
    Well, the IDE usually highlights the current brace and its ending one if you type one out.
    Besides that, selecting all and using auto-indentation (Alt+F8 with VC++ 6 scheme), it will go nuts when there are mismatches.

    Code:
    Say I had:
    
    Code:
    LineData[0].string2 = "bobthebuilder.html"
    How could I achieve something like
    Code:
    fopen("c:\website\%s",LineData[0].string2, "rt");
    Thanks in advance. :)
    Prepare the string first using say, sprintf, to build the full path, then pass it to fopen.

    Quote Originally Posted by happyclown View Post
    I wonder if there is a way for me to see what it is doing during those 14 seconds? I'll try to figure this one out. And will post back here if I still need help.
    Use the debugger?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Brace matching
    By coskun in forum C Programming
    Replies: 6
    Last Post: 07-30-2004, 06:45 AM

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