the misterious case of the non-migrating function...

This is a discussion on the misterious case of the non-migrating function... within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I am trying to migrate my app from Windows to Linux (since C++ is sooooo portable...), and there is ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    5

    the misterious case of the non-migrating function...

    Hi,

    I am trying to migrate my app from Windows to Linux (since C++ is sooooo portable...), and there is this one function that gives me the following error. I checked the sintax, itīs Ok. I searched through the entire C forums... and to avail. Here is the error:

    Filetest.cpp: In function `int addCredit(char*, long int)':
    Filetest.cpp:311: you are requesting the convertion from `int' to a non-escalar type `_G_fpos_t'
    Filetest.cpp:341: no match for the operador `fpos_t& = int'
    /usr/include/_G_config.h:27: candidates are: _G_fpos_t&
    _G_fpos_t:perator=(const _G_fpos_t&)

    Here is the code (basic stuff, I know)

    Code:
    int addCredit(char *number_in, long amount)
    {
    	FILE *stream;
    	int count = 0;
    	fpos_t pos = 0;
    	long temp = 0;
    
    	char account_in[10];
    	char name_in[20];
    	char last_in[20];
    	long balance = 0;       // length of the record is 66 bytes (get it from
    							// properties of the file after writing the first record)
    
    
    	printf("Buscando: %s\n",number_in);
    	// Attempt to open the file. If it returns NULL fails. exit with condition 1
    	
        if( (stream = fopen( FullFileName, "r+" )) == NULL )
          return 1 ;
    
        /* Cycle until end of file reached:or record found */
        while( !feof( stream ) )
    	{  
    		fscanf(stream,"%s  %s  %s  %d\n",account_in, name_in, last_in, &balance);
    		if( ferror( stream ) )      
    		{
    			perror( "Read error" );
    			break;
    		}
    		if ((strcmp(account_in,number_in)) == 0) // if they are identical
    		{
    			printf("Encontrado...");
    
    			// retroceder puntero para seņalar al registro correcto
    			pos = (66 * count);   // length of record times nmbr of previous records
    			fsetpos( stream, &pos );
    
    			// ------------- modify amount and save it -----------------
    			temp =  balance;				// Restar del balance
      		 	balance = (temp + amount);	// Actualizar el balance
        		fprintf(stream,"%10s  %20s %20s %10ld\n",account_in, name_in, last_in, balance);
    
    			fclose(stream);
    			return 0;
    		}
    		//  Count number of records read 
    		count = count + 1;
    	}
    
    	printf("Registro no existe. Total registros: %d\n",count);
    
    	fclose(stream);
    	return 1;
    any help would be greatly appreciated

    Luis

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Nothing looks wrong right off the bat to me, mabey if you threw in line number and we could pinpoint the problem more.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Things I have noticed:
    Code:
    if( (stream = fopen( FullFileName, "r+" )) == NULL )
    Where is FullFileName defined? What is its value here?
    Code:
    while( !feof( stream ) )
    FAQ > Explanations of... > Why it's bad to use feof() to control a loop

    You are returning 1 regardless if you succeed or not

    What destro and what compiler?

    Have you tried casting?
    Code:
    fsetpos( stream, (fpos_t *)&pos );
    And if this is a C++ program why aren't you using fstreams?

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Thanks for the interest. Here are some answers:

    Where is FullFileName defined? What is its value here?

    Itīs defined as a global with the name of the file. The value is "C:\production\clients.txt"

    What destro and what compiler?

    It's the gnu distro that comes the redhat 9.0

    Line 311 is this one:

    fpos_t pos = 0;

    Line 341 is this one:

    pos = (66 * count);

    BTW: This program compiled just fine (in fact, it was in production) under Visual C++ 6.0

    Thanks in advanced.

    Luis

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well one thing is that the file name isn't going to be valid under unix

    the other part is that in my version fpos_t is used as a typedef for _G_fpos_t or _G_fpos64_t

    Let me see what I can piece together

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Looking over it some more the entire function is written in C code so try compiling it using gcc instead of g++. Its worth a shot

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    5

    you are closer on the first answer

    I think that you may have gotten pretty close to the answer when you said something about the "typedef". Because as you can see, the error says something to the effect that the variables are incompatible, either here:

    fpos_t pos = 0;

    or here: pos = (66 * count);

    The thing is, I need a variable so that I can back-up the file pointer to the beginning of the record, so that I can rewrite it.

    And yes, I already tried compiling it with gcc.

    Thanks for all the help.

    Luis

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    > I am trying to migrate my app from Windows to Linux (since C++ is sooooo portable...)
    You mis-understand portable then.
    1. simply writing it in C or C++ does not make it portable.
    2. the fact that it works on one implementation does not make it portable.

    > pos = (66 * count); // length of record times nmbr of previous records
    > fsetpos( stream, &pos );
    fpos_t is an opaque type, you don't know what is really underneath. The only thing you can do is initialise it with fgetpos and use it with fsetpos. Anything else is wrong and non-portable.
    Code:
    fgetpos( fp, &pos );
    // some code here
    fsetpos( fp, &pos );
    So, I think your code should be like this - I've also fixed your feof() problem as well
    Code:
    int addCredit(char *number_in, long amount)
    {
        char buff[BUFSIZ];
        FILE *stream;
        int count = 0;
        fpos_t pos;
    
        char account_in[10];
        char name_in[20];
        char last_in[20];
        long balance = 0;
    
        printf("Buscando: %s\n", number_in);
        // Attempt to open the file. If it returns NULL fails. exit with condition 1
    
        if ((stream = fopen(FullFileName, "r+")) == NULL)
            return 1;
    
        /* Cycle until end of file reached:or record found */
        fgetpos( stream, &pos );  // the initial position
        while ( fgets( buff, sizeof buff, stream ) != NULL ) {
            int numvals;
    
            // improve sscanf call, so you don't buffer overflow your strings.
            numvals = sscanf(buff, "%s %s %s %d", account_in, name_in, last_in, &balance);
            if ( numvals != 4 ) {
                // some error message
            }
    
            if ((strcmp(account_in, number_in)) == 0)   // if they are identical
            {
                printf("Encontrado...");
                fsetpos(stream, &pos);  // back to start of record
    
                // ------------- modify amount and save it -----------------
                fprintf(stream, "%10s  %20s %20s %10ld\n", account_in, name_in,
                        last_in, balance + amount);
                fclose(stream);
                return 0;
            }
    
            // now pointing at the start of the next record
            fgetpos( stream, &pos );
            //  Count number of records read
            count = count + 1;
        }
    
        printf("Registro no existe. Total registros: %d\n", count);
    
        fclose(stream);
        return 1;
    }
    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.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    5

    Thanks very much

    Salem:

    Thanks very much. I'll try that code right away and let u know. sorry I did not get back to you sooner, but I am left with a dial-up connection. My dsl won't be up until next week.

    Thanks again.

    Luis

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Hey Salem, your code worked like a charm, the first time, no compile errors or nothing. Man, you are a Godsend. Thanks very much.

    Luis

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Changing bkgrnd color of Child windows
    By cMADsc in forum Windows Programming
    Replies: 11
    Last Post: 09-10-2002, 11:21 PM

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