Using first character from a file

This is a discussion on Using first character from a file within the C Programming forums, part of the General Programming Boards category; I am trying to read the first character (interger) from a file, and use that number as the value of ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    Using first character from a file

    I am trying to read the first character (interger) from a file, and use that number as the value of the number of characters there are in the file, telling this to the code, so it knows how much more to read. I can find this number by starting at the beginning, seeking the End Of File, and rewinding so I am back to the beginning:

    Code:
    int main () {
      FILE * testFile;
      const char FileSize;
      char * buffer;
      size_t result;
    
      testFile = fopen ( "trying.dat" , "rb" );
      
      fseek (testFile , 0 , SEEK_END);
      FileSize = ftell (testFile);
      rewind (testFile);
    but is there a way I can just look at the first character and store it as the number of total characters in the file? Thank you in advance!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you mean that if the first character in the file is '9' you should have nine (more or total) characters; or if the first character is 0x09 (CTRL-I, or equivalent) you should have nine (more or total) characters; or if the file starts with "35" that there should be 35 (more or total) characters?

    (Obviously nine is the limit for the first case.)

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21
    Correct, if the first character is 9, then it somehow knows there are 9 characters total in the file it is reading from. Just not sure how to make this happen.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You use fscanf.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    21
    The setup for fscanf is:
    Code:
    int fscanf( FILE *stream, const char *format, ... );

    the code I tried is:

    Code:
    int main(int argc, char *argv[])
    {
    int numberOfChar;
       	FILE *fp;
    	fp=fopen("test.txt", "r");
    	fscanf(fp, numberOfChar, 0);
        return 0;
    }
    and

    Code:
    int main(int argc, char *argv[])
    {
    int numberOfChar;
       	FILE *fp;
    	fp=fopen("test.txt", "r");
    	fscanf(fp, 0, numberOfChar);
        return 0;
    }
    the first does nothing, the second closes the exe and returns a large negative number. Am I following the fscan format wrong?

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Code:
    if (fscanf(fp, "%d", &numberOfChar) == 1)
    {
        // Successfully read number of characters into 
        // numberOfChar variable.
    }

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Hold it, hold it, hold it. Is the first character to be interpreted as an integer as stated in the first post? Zero to 255? As opposed to an ASCII string of a possibly multi-digit number? Let's nail that down first.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    21
    Just a single integer from 0-255. The numbers in the file are: 4 25 34 56 70

    Code:
    if (fscanf(fp, "%d", &numberOfChar) == 1)
    printf("%d\n", numberOfChar);
    Does correctly read and print (just to check) the first character. Now I just need to use this value.

    I tried:

    Code:
    int main(int argc, char *argv[])
    {
    	int numberOfChar;
    	int myint;
    	int currentInt=0;
    
       	FILE *fp;
    	fp=fopen("test.txt", "rb");
    	if (fscanf(fp, "%d", &numberOfChar) == 1)
    	printf("%d\n", numberOfChar); //just to make sure it read correctly, and it did
    
    for(currentInt = 0; currentInt < numberOfChar; currentInt++);	
    	{
    	fscanf(fp, "%d", &myint);
    	printf("%d\n", myint);
            }
    
    
     return 0;
    }

    This returns:

    4
    25

    I also tried:
    Code:
    int main(int argc, char *argv[])
    {
    	int numberOfChar;
    	int myint;
    	int currentInt=0;
    
       	FILE *fp;
    	fp=fopen("test.txt", "rb");
    	if (fscanf(fp, "%d", &numberOfChar) == 1)
    	printf("%d\n", numberOfChar); //just to make sure it read correctly, and it did
    
    	while (currentInt < numberOfChar)
    	{
    		fscanf(fp, "%d", &myint);
    		printf("%d\n", myint);
    		myint ++;
    	}
    
     return 0;
    }
    This gives me an infinite loop. I tried with &s in front of the currentId and or the numberOfChar in the while loop, which gave me the error of "< not compatible with int and int."

    If I just repeat:

    Code:
    ...
    
    fscanf(fp, "%d", &myint);
    printf("%d\n", myint);
    
    fscanf(fp, "%d", &myint);
    printf("%d\n", myint);
    
    fscanf(fp, "%d", &myint);
    printf("%d\n", myint);
    
    ...
    It returns:

    20

    25

    34

    56

    So, I know the:

    Code:
    fscanf(fp, "%d", &myint);
    printf("%d\n", myint);
    sequence needs to be repeated, is there a special way to do this with files? Like placing file pointers in the loops? Thanks again!

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So is the file human-readable? (I mean if you look at it, do you see four different numbers?) If so, you probably just want "r" instead of "rb". I don't think it matters in this case, but in general.

    Also, note the semicolon after your for loop, which makes your for-loop empty! Exciting!

    Edit: And note that you are not using the first character to decide how many numbers, but the first integer -- if the file started "10 1 2 3 4 5 6 7 8 9 10", you wouldn't just use the first character 1, you'd use the whole first number 10.

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    21
    Wow, I can't believe I didn't see the semicolon. After all these years...that's alright everyone makes mistakes, and I thank you all for helping me! Works great now!

    Finished Product:

    Code:
    int main(int argc, char *argv[])
    {
    
    	int numberOfChar;
    	int myint;
    	int currentInt=0;
    
       	FILE *fp;
    	fp=fopen("test.txt", "rb");
    	if (fscanf(fp, "%d", &numberOfChar) == 1)
    	printf("%d\n", numberOfChar);
    for(currentInt = 0; currentInt < numberOfChar; currentInt++)
    	{
    	fscanf(fp, "%d", &myint);
    	printf("%d\n", myint);
    	
    }
    
     return 0;
    }

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You might want to print out some error message and terminate the program early if the first count does not come in right. You're already checking, but you've got no contingency plan.

    Or, be sure to initialize numberOfChar to 0 at the top. That way the for loop doesn't get executed if there was a failed first number.

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    21

    Smile

    Thanks to you all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM

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