Click here to Skip to main content
15,885,925 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Question :Use 3.asc file and s.fit in C program to extract a period corresponding to the numbers in s.fit from 3.asc and write to ASCII output file.

Two files named s.fit and 3.asc are given.
The file s.fit contains certain numbers like 399, 627, 786,976...,17865.(10 nums).(this numbers are give in each line, in a column fashion).
The next file 3.asc file contains 17900 period , each period containing numbers from 1 to 512 and corresponding to each number there is a decimal number as shown below .
1 0.3455 #1st period
2 2.675
. 5.98
. 4.654
. 45566
512 6.22345
1 8.977 #2nd priod
2 .
3 . .
4 .
5
. .
. .
. .
. 9.676
512 0.6584
. #321 period
.
. .
. #627 th period

.
. .
. .
. # 786 period
.
.
. .
.
. etc

so the task assigned to me is to write a c program satisfying these conditions.
please help me..

What I have tried:

C++
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int split( char * str, char delim, char ***array, int *length ) {
  char *p;
  char **res;
  int count=0;
  int k=0;

  p = str;
  
  while( (p=strchr(p,delim)) != NULL ) {
    *p = 0; 
    p++; 
    count++;
  }

  // allocate dynamic array
  res = calloc( 1, count * sizeof(char *));
  if( !res ) return -1;

  p = str;
  for( k=0; k&lt;count;&gt;    if( *p ) res[k] = p;  
    p = strchr(p, 0 );    
    p++; 
  }

  *array = res;
  *length = count;

  return 0;
}

#define BUFFER_SIZE 1024

int main() {

  const char *delimiter_characters = "\n";
    const char *delimiter_characters2 = " ";
    const char *filename1 = "C:\\user\\hope\\s.fit";
    const char *filename2 = "C:\\user\\hope\\3.asc file ";
    FILE *input_file_1 = fopen( filename1, "r" );
    FILE *input_file_2 = fopen( filename2, "r" );
    char buffer[ BUFFER_SIZE ];
    char buffer2[ BUFFER_SIZE ];
    char buffer_lines[ BUFFER_SIZE ];
    char *last_token;
    char *last_token2;
    int result, errno;

  char **res;
  int k=0;
  int *count =0;
  int rc;

  if( input_file_1 == NULL ){

        fprintf( stderr, "Unable to open file %s\n", filename1 );

    }
    else if( input_file_2 == NULL ){

        fprintf( stderr, "Unable to open file %s\n", filename2 );

    }
    else{
         while( fgets(buffer, BUFFER_SIZE, input_file_1) != NULL ){
                last_token = strtok( buffer, delimiter_characters );

            while( fgets(buffer2, BUFFER_SIZE, input_file_2) != NULL ){
                rc = split( buffer2, ' ', &res, &count );
              if( rc ) {
                printf("Error: %s errno: %d \n", strerror(errno), errno);
              }
              printf("count: %d\n", count );
               //if(strcmp(last_token, res[0]) == 0){
                 for( k=0; k&lt;count;&gt;                         printf("str: %s\n", res[k]);
                 }
                //break;
               //}
              }
              rewind(input_file_2);
        }
 }
  return 0;
}
Posted
Updated 1-May-16 6:16am
v2
Comments
OriginalGriff 30-Apr-16 8:24am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to make it right?
Use the "Improve question" widget to edit your question and provide better information.
Patrice T 1-May-16 12:28pm    
And ? what is the problem in this code ?
What is your question?

1 solution

To make your code more efficient, it is better first to read the entire number list from s.fit file, then sort it and extract the requested periods from 3.asc file in a single pass:

C
#include <stdlib.h>
#include <stdio.h>

#define BUFFER_SIZE 1024
#define NUM 10 // Maximum number of items in s.fit file
#define PERIODNUM 17900
#define LINESPERPERIOD 512

int compare(const int *arg1, const int *arg2)
{
	return *arg1 == *arg2 ? 0 : *arg1 < *arg2 ? -1 : 1;
}

void extract() {
	const char *filename1 = "C:\\user\\hope\\s.fit";
	const char *filename2 = "C:\\user\\hope\\3.asc";
	const char *filename3 = "C:\\user\\hope\\3.out";
	FILE *input_file_1 = fopen(filename1, "r");
	FILE *input_file_2 = fopen(filename2, "r");
	FILE *output_file = fopen(filename3, "w");
	char buffer[BUFFER_SIZE];
	int i = 0, n = 0;
	int numbers[NUM];
	char* res;

	if (input_file_1 == NULL){
		fprintf(stderr, "Unable to open file %s\n", filename1);
	}
	else if (input_file_2 == NULL){
		fprintf(stderr, "Unable to open file %s\n", filename2);
	}
	else {
		// Read s.fit file
		while (n < NUM && fgets(buffer, BUFFER_SIZE, input_file_1) != NULL)
			numbers[n++] = atoi(buffer);
		fclose(input_file_1);
		qsort(numbers, n, sizeof(int), compare); // Sort numbers
		// Extract and output requested periods
		for (int p = 1; p <= PERIODNUM && i<n; p++) {
			if (numbers[i] == p) fprintf(output_file, "Period: %d\n", p);
			for (int l = 1; l <= LINESPERPERIOD; l++)
				if ((res=fgets(buffer, BUFFER_SIZE, input_file_2)) != NULL) {
					if (numbers[i] == p) fputs(buffer, output_file); // Preprocess buffer if needed
				}
				else break;
			if (res == NULL) break;
			if (numbers[i] == p) i++; // Proceed to the next period number
		}
		if (res != NULL) puts("Processing completed successfully\n");
			else puts("Not enough lines in 3.asc file\n");
	}
	fclose(input_file_2);
	fclose(output_file);
}


This code is just a sketch of what your final program may look like. You'll need to add proper error processing, for example, as the only error checked here is too short a 3.asc file. Also, it is not clear what data you need to put in the output file so this code sample just puts the entire line from the 3.asc file. You may add necessary processing of this line in the place indicated by the comment.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900