Replace Array

This is a discussion on Replace Array within the C Programming forums, part of the General Programming Boards category; This is a short program I have been working on.....I have got it to open a binaryfile...however I would like ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    Replace Array

    This is a short program I have been working on.....I have got it to open a binaryfile...however I would like to use a function called replace (void replace (double *array, int size)) that will replace the smallest element in the array with -9999.0. All help would be appreciated!

    Code:
    #include <stdio.h>
    
    int
    main (void)
    {
            FILE *binaryfile;
            int i,x;
            
            binaryfile = fopen ("numbers.bin", "rb");
     
            for (i=1; i<=10; ++i)
            {
                   fread (&x, sizeof(int), 1, binaryfile);
                   printf ("%d ", x);
            }
     
            fclose (binaryfile);
     
            return (0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So write a function to read through an array and locate the smallest value.
    Now go to that spot in the array, replace your value, do whatever it is you need to with the array.
    Your code isn't really an attempt at anything. All you're doing is showing you know how to open a file.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    My choice for reading the data would be different.
    Code:
     double array[10];
          size_t i, count = fread(array, sizeof *array, sizeof array / sizeof *array, file);
          fclose(file);
          replace(array, count);
          for ( i = 0; i < count; ++i )
          {
             printf("%g\n", array[i]);
          }
    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.*

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    Ok.....I have added more to my program..I try to get myself to do the basics then work from there so I had to ask the user to input a file...then read the file and replace the smallest double with
    -9999.0 The only error I'm geting is " c:52 file does not end in a new line" Any advice? Please! As you see that bottom is a mess!!


    Code:
    #include <stdio.h>
    
    int
    main (void)
    {
    
    	void fileio (void);
    	{
    	
    	char USERINPUT[100] 
    	double RECORD[100];
    
    	
    	FILE *BINARYFILE;
    
    	printf	("Enter a file to be opened?\n");
    	scanf	("%[^\n]", &USERINPUT);
    
    	
    	if ( ( BINARYFILE = fopen(USERINPUT, "rb" ) ) == NULL)
    		{
    		printf( "Sorry! The file could not be opened\n" );
    		} 
    	else 
    		{
    		
    		printf	("%s\n", USERINPUT);
    		fscanf	(BINARYFILE, "%[^\n]", RECORD);
    
    		while ( !feof(BINARYFILE)) 
    			{
    			printf	("%s\n", RECORD);
    			fscanf	(BINARYFILE, "%s[^\n]", RECORD);
    			}
    	 
    	  size_t i; 
    	  count = fread(RECORD, sizeof *RECORD, sizeof RECORD / sizeof *RECORD, BINARYFILE);
     	  fclose(BINARYFILE);
          replace(RECORD, count);
          for ( i = 0; i < count; ++i )
          {
             printf("%g\n", RECORD[i]);
          }
    
    		} 
    	}
    return(0);
    }
    Last edited by SARAHCPS; 11-14-2005 at 01:23 PM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    In regard to the message -- go to the end of the file and press the return key, then save the file.

    But do also visit the FAQ to find a better way to read strings. And also about not using feof for loop control. And why are you trying to read your binary file with fscanf?

    Are you using C99 or 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.*

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    I'm using 99! I added alittle more to see if I can get the value replaced...I'm still getting the same error!

    Code:
    #include <stdio.h>
    
    {
    
    	void fileio (void);
       {
    	
    	char USERINPUT[100] 
    	double RECORD[100];
    
    	
    	FILE *BINARYFILE;
    
    	printf	("Enter a file to be opened?\n");
    	scanf	("%d", &USERINPUT);
    
    	
    	if ( ( BINARYFILE = fopen(USERINPUT, "rb" ) ) == NULL)
    		{
    		printf( "Sorry! The file could not be opened\n" );
    		} 
    	else 
    		{
    		
    		printf	("%s\n", USERINPUT);
    		fscanf	(BINARYFILE, "%d", RECORD);
    
    		while ( !feof(BINARYFILE)) 
    			{
    			printf	("%s\n", RECORD);
    			fread	(BINARYFILE, "%d", RECORD);
    			}
    
    	void replace (RECORD[], char RECORD2[])
     {
      int i, j, length;
      i = -9999.0
      for (j=0;j<length; ++j)
        { 
         RECORD[j] = RECORD2[i];
         --i;
        }
       printf("%s\n", RECORD);
    		}
    
    	return (0); 
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Is it really an "error", or just a "warning"?
    Code:
       /* code */
    	return (0); 
    } <press the return key here, maybe twice for good measure, then save the file>
    [edit]Wait, I see. Your indentation is horrible. [Newbs really ought to be required to learn indentation first.] This is that you have:
    Code:
    #include <stdio.h>
    
    {
    
       void fileio (void);
       {
    
          char USERINPUT[100]
          double RECORD[100];
    
    
          FILE *BINARYFILE;
    
          printf   ("Enter a file to be opened?\n");
          scanf ("%d", &USERINPUT);
    
    
          if ( ( BINARYFILE = fopen(USERINPUT, "rb" ) ) == NULL )
          {
             printf( "Sorry! The file could not be opened\n" );
          }
          else
          {
    
             printf   ("%s\n", USERINPUT);
             fscanf   (BINARYFILE, "%d", RECORD);
    
             while ( !feof(BINARYFILE) )
             {
                printf   ("%s\n", RECORD);
                fread (BINARYFILE, "%d", RECORD);
             }
    
             void replace (RECORD[], char RECORD2[])
             {
                int i, j, length;
                i = -9999.0
                    for ( j=0;j<length; ++j )
                    {
                       RECORD[j] = RECORD2[i];
                       --i;
                    }
                    printf("%s\n", RECORD);
             }
    
             return(0);
          }
    Last edited by Dave_Sinkula; 11-14-2005 at 01:48 PM.
    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.*

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    Sorry this must be annoying!! I'm still getting the same error!

    lab9.c:52:file does not end in a new line

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Start with this shell.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void replace(double *array, size_t size)
    {
       /* your code here */
    }
    
    iint main(void)
    {
       FILE *file;
       /*
        * Get the filename.
        */
       char filename[100];
       fputs("filename? ", stdout);
       fflush(stdout);
       if ( fgets(filename, sizeof filename, stdin) != NULL )
       {
          char *newline = strchr(filename, '\n');
          if ( newline != NULL )
          {
             *newline = '\0';
          }
       }
       /*
        * Try to open the file.
        */
       file = fopen(filename, "rb");
       if ( file != NULL )
       {
          /*
           * Read the data into 'array'.
           */
          double array[10];
          size_t i, count;
          count = fread(array, sizeof *array, sizeof array / sizeof *array, file);
          fclose(file);
          /*
           * Do stuff.
           */
          replace(array, count);
          for ( i = 0; i < count; ++i )
          {
             printf("%g\n", array[i]);
          }
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    [edit]And read these:
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    FAQ > Explanations of... > Why it's bad to use feof() to control a loop
    Last edited by Dave_Sinkula; 11-14-2005 at 02:06 PM.
    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.*

  10. #10
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    > lab9.c:52:file does not end in a new line
    Gee, I wonder....

    D'ya think going to the end of the code, pressing return and saving it would help.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM

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