Thread: file reading problems again

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    file reading problems again

    Ive tried using fgets and fscanf, but neither recognise a spaces, they regard them as being the end of the input.

    Are there anything else i can use? Below is a copy of one of my read in lines.

    fgets(&MemberData[counter1].membernumber,100, memberRead); // read in the data

    BlueBob

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fgets(&MemberData[counter1].membernumber,100, memberRead);
    This should read spaces just fine

    It will only stop when
    - the buffer is full
    - it read the \n character
    - it reached the end of file (or there was a file error)

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    fgets reads up to the first newline '\n' or the first nth chars (whatever you specified for the length ). Thus, it should read past your spaces....have you actually looked at the file your reading?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    well it dont seem to work. The data is written correctly, but when it is read in, it is not firmatted properly. Their are massive gaps between fields, and some data is not displayed. This has been bugging me for ages, i just cant seem to solve it.

    BlueBob

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Better attach some code and data files (say in a zip file).

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, be sure you're not opening the file for binary reading ( "rb" ), but in the plain old "r" mode...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    the files are opened using 'r', and closed using 'w'.

    The data is written correctly,

    mem1
    Blue Bob
    C Board
    0

    But when it is read back in, and printed out, a new line is taken after each piece of data, and random numbers are printed inplace of the '0'.

    Any ideas?

    for(counter1 = 0; counter1 < m; counter1++)
    {
    fprintf (memberWrite, "%s\n", MemberData[counter1].membernumber);
    } // to write

    while (!feof(memberRead))
    {
    fgets(&MemberData[counter1].membernumber,10, memberRead); // to read

    Any ideas? BlueBob

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by bluebob

    fgets(&MemberData[counter1].membernumber,10,
    without seeing more of your code, I'd guess the problem is here. What is MemberData[counter1].membernumber defined as? If it's a char array then you don't need the & in front of the name. THis is because fgets takes a char pointer as its first arg. The name of a char array (char buffer[10]) is all you need to pass.

    Also, I believe the loop method in the below example is slightly better. You can always check the value of errno if the fgets call fails.

    Example:

    Code:
    #include <stdio.h>
    int main(void)
    {
    	FILE *fp;
    	char buf[100];
    	if ((fp = fopen("junk6.c", "r")) == NULL)
    	{
    		perror("file");
    		return (1);
    	}
    	
    	while (fgets(buf, sizeof(buf), fp))
    		printf("%s", buf);
    	return(0);
    }
    Lastly, the newline will be kept in the array, but this is in my documentation, so be warned:

    A common programming error is to assume the presence of a new-line character in every string that is read into the array. A new-line character will not be present when more than n-1 characters occur before the new-line. Also, a new-line character might not appear as the last character in a file, just before end-of-file
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by bluebob
    But when it is read back in, and printed out, a new line is taken after each piece of data, and random numbers are printed inplace of the '0'.
    Sorry, just read this bit again, and understood what you meant

    The extra random numbers are probably caused by the use of feof(). Do the loop as given in my previous post. This is because feof() will only be true if you have performed a read and failed. In that case, your buffer will probably be empty (or invalid), and the method you are using would suggest you are using the duff buffer as output.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. I have some problems reading struc from file
    By samc2004 in forum C Programming
    Replies: 4
    Last Post: 12-18-2003, 02:47 PM