why is the charcter constant?

This is a discussion on why is the charcter constant? within the C Programming forums, part of the General Programming Boards category; Here is what I have... Code: #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 500000 int main(void) { int i ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    56

    why is the charcter constant?

    Here is what I have...

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <time.h>
    
    #define SIZE 500000 
    
    int main(void) 
    { 
      
      int i = 0, b = 0, d = 0; 
      FILE *fp1; 
      fp1 = fopen("c:\\blah.html", "r"); 
      
    
      char html[SIZE]; 
      char url[150][512]; 
      char c; 
      int count = 0; 
      
      fread(html, sizeof(char), SIZE, fp1); 
     
      for (c = html[i]; c != sizeof(html); i++)
      { 
       if (c == '\"') 
        { 
         count++;
          continue; 
        } 
        if (count == 1) 
        { 
    	
    		url[b][d] = c;
    		printf("%c", url[b][d]);
    		d++;
    	} 
    	if (count == 2)
    	{
    		url[b][d] = '\0';
    		printf("\n");
    		b++;
    		count = 0;
    	}
    	
      } 
      
      fclose(fp1); 
    
    
      return 0; 
    }
    For some reason, when I check with the debugger, the variable c, does not change. The html[i], changes, but I don't understand why c does not change if it is always assigned a new character. Any help is appreciated.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
      for (c = html[i]; c != sizeof(html); i++)
    Do your chars hold values up to 500000?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    oops, yeah it's not supposed to be that large. But I don't think that is the casue of the problem.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Besides the loop initialization, when do you assign a new value to c?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    it never changes because its only set once -- when the loop starts. After that, c is never reset after incrementing i.
    Code:
    for (c = html[i]; i <  sizeof(html); i++)
    {
        c = htm[i];
        // rest of your code here
    }
    That is really a badly formatted loop anyway. you should start by setting i to 0 or something, like this:
    Code:
    for (i=0; i <  sizeof(html); i++)
    {
        c = htm[i];
        // rest of your code here
    }
    Last edited by Ancient Dragon; 10-13-2005 at 08:02 PM.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    Oh, gotcha, thanks a bunch.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > fread(html, sizeof(char), SIZE, fp1);
    > for (c = html[i]; c != sizeof(html); i++)
    Who said the fread() filled the array exactly?
    What if there is only 1278 bytes of HTML data, what happens then?
    What if theres 12345678 bytes of HTML data, and byte 500000 happens to be a " ?

    > fp1 = fopen("c:\\blah.html", "r");
    This is between two declarations, you slipped into C++ or C99 coding.
    Also, where is the error checking?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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