Hey guys. So I basically read in a bunch of data from a file into a data structure. But while I was doing that I included the spaces (which I dont want). So now I want to compare each element to see if it does have a blank space, and if it does i want to ignore it by incrementing some random variable, and if it isnt I want to put it into a new array so I could print the contents of the file without space. I realize that this is bad coding and there must be a different way to do it, any suggestions? This is my code. Im not really good with data structures so thats why im struggling a little bit. *c programming

This is an example input file:

Gates,                 William           ,H,1.89
Jobs,                   Steven,        P,2.56
Brin   , Sergey   ,     ,    3.89
                Page, Lawrence        ,,        3.77
         Buffett, Warren, E, 3.34

Expected output file:
William H. Gates 1.89
Steven P. Jobs 2.56
Sergey Brin 3.89
Lawrence Page 3.77
Warren E. Buffett 3.34

My code:

#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#define TEN 10
#define THREE 3
#define TWENTY 20
#define FIFTY 50
#define ONE_THOU 1000

typedef struct {
char first_name[TEN];
char middle_name[TWENTY];
char last_name[TWENTY];
double users_gpa;
} format_of_file;

int main () {
	/* Declarations: File I/O */
  FILE *inp, *outp;
	char first_file[FIFTY],second_file[FIFTY],str[ONE_THOU],*ptr;
	format_of_file organized[ONE_THOU];
	int i, count;
	double sum,min_value,max_value,average_value;

	/* Get users inputs */
	printf("Enter the input file name: ");
	scanf("%s", first_file);
	printf("Enter the output file name: ");
	scanf("%s", second_file);

	inp = fopen(first_file, "r");
	outp = fopen(second_file, "w");

	/* Test if both files for validity */
	if (inp == NULL) {
		perror("Invalid File");
		exit(EXIT_FAILURE);
	}
	if (outp == NULL) {
		perror("Invalid File");
		exit(EXIT_FAILURE);	
	}

  /* Begin Program execution and get each name/gpa until , and then continue */
  count = 0;
	while(fgets(str,sizeof(str),inp) != NULL) {
	  if ((ptr = strtok(str,",\n")) != NULL) {
      strcpy(organized[count].last_name,ptr);
    }
    else {
      organized[count].last_name[0]='\0';
    }
    if ((ptr = strtok(NULL,",\n")) != NULL) {
      strcpy(organized[count].first_name,ptr);
    }
    else {
      organized[count].first_name[0]='\0';
    }
    if ((ptr = strtok(NULL,",\n")) != NULL) {
      strcpy(organized[count].middle_name,ptr);
    }
    else {
      organized[count].middle_name[0]='\0';
    }
    if ((ptr = strtok(NULL,",\n")) != NULL) {
      organized[count].users_gpa=atof(ptr);
    }
    else {
      organized[count].users_gpa=0;
    }  
			count++;
			
		/* Check to see if the size of the file is under One thousand*/
  	if (count > ONE_THOU) {
	    printf("Error with file size\n");
	    return 0;
  	}
	}
	/* Close first File */
  fclose(inp);
	
	/* Find the highest/lowest value/sum */
	max_value = organized[0].users_gpa;
	min_value = organized[0].users_gpa;
	sum = 0;
	for (i=0;i < count;i++) {
	  if (max_value < organized[i].users_gpa) {
	    max_value = organized[i].users_gpa;
	  }
	  if (min_value > organized[i].users_gpa) {
	    min_value = organized[i].users_gpa;
	  }
	  sum = sum + organized[i].users_gpa;
	}
	
	/* Find the average value */
	average_value = (sum/count);
  printf("%d\n", count);
  /* Display Results */
  fprintf(outp,"Highest gpa: %.2f\n", max_value);
  fprintf(outp,"Average gpa: %.2f\n", average_value);
  fprintf(outp,"Lowest gpa: %.2f\n\n", min_value);
  
  for (i=0;i < count;i++) { 
    fprintf(outp,"%s ", organized[i].first_name);
      
    fprintf(outp,"%s. ", organized[i].middle_name);
    
    fprintf(outp,"%s ", organized[i].last_name);

    fprintf(outp,"%.2f\n", organized[i].users_gpa);
  }
  
  /* Close files */
  fclose(outp);

  return 0;
}

Please use code tags ...

It is difficult to read your code.

Sorry im new to this site. Here is the code with the code tags

#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#define TEN 10
#define THREE 3
#define TWENTY 20
#define FIFTY 50
#define ONE_THOU 1000

typedef struct {
char first_name[TEN];
char middle_name[TWENTY];
char last_name[TWENTY];
double users_gpa;
} format_of_file;

int main () {
	/* Declarations: File I/O */
  FILE *inp, *outp;
	char first_file[FIFTY],second_file[FIFTY],str[ONE_THOU],*ptr;
	format_of_file organized[ONE_THOU];
	int i, count;
	double sum,min_value,max_value,average_value;

	/* Get users inputs */
	printf("Enter the input file name: ");
	scanf("%s", first_file);
	printf("Enter the output file name: ");
	scanf("%s", second_file);

	inp = fopen(first_file, "r");
	outp = fopen(second_file, "w");

	/* Test if both files for validity */
	if (inp == NULL) {
		perror("Invalid File");
		exit(EXIT_FAILURE);
	}
	if (outp == NULL) {
		perror("Invalid File");
		exit(EXIT_FAILURE);	
	}

  /* Begin Program execution and get each name/gpa until , and then continue */
  count = 0;
	while(fgets(str,sizeof(str),inp) != NULL) {
	  if ((ptr = strtok(str,",\n")) != NULL) {
      strcpy(organized[count].last_name,ptr);
    }
    else {
      organized[count].last_name[0]='\0';
    }
    if ((ptr = strtok(NULL,",\n")) != NULL) {
      strcpy(organized[count].first_name,ptr);
    }
    else {
      organized[count].first_name[0]='\0';
    }
    if ((ptr = strtok(NULL,",\n")) != NULL) {
      strcpy(organized[count].middle_name,ptr);
    }
    else {
      organized[count].middle_name[0]='\0';
    }
    if ((ptr = strtok(NULL,",\n")) != NULL) {
      organized[count].users_gpa=atof(ptr);
    }
    else {
      organized[count].users_gpa=0;
    }  
			count++;
			
		/* Check to see if the size of the file is under One thousand*/
  	if (count > ONE_THOU) {
	    printf("Error with file size\n");
	    return 0;
  	}
	}
	/* Close first File */
  fclose(inp);
	
	/* Find the highest/lowest value/sum */
	max_value = organized[0].users_gpa;
	min_value = organized[0].users_gpa;
	sum = 0;
	for (i=0;i < count;i++) {
	  if (max_value < organized[i].users_gpa) {
	    max_value = organized[i].users_gpa;
	  }
	  if (min_value > organized[i].users_gpa) {
	    min_value = organized[i].users_gpa;
	  }
	  sum = sum + organized[i].users_gpa;
	}
	
	/* Find the average value */
	average_value = (sum/count);
  printf("%d\n", count);
  /* Display Results */
  fprintf(outp,"Highest gpa: %.2f\n", max_value);
  fprintf(outp,"Average gpa: %.2f\n", average_value);
  fprintf(outp,"Lowest gpa: %.2f\n\n", min_value);
  
  for (i=0;i < count;i++) { 
    fprintf(outp,"%s ", organized[i].first_name);
      
    fprintf(outp,"%s. ", organized[i].middle_name);
    
    fprintf(outp,"%s ", organized[i].last_name);

    fprintf(outp,"%.2f\n", organized[i].users_gpa);
  }
  
  /* Close files */
  fclose(outp);

  return 0;
}

speedy94519> So now I want to compare each element to see if it does have a blank space, and if it does i want to ignore it by incrementing some random variable, and if it isnt I want to put it into a new array so I could print the contents of the file without space.

Still wondering about what you want, nevertheless, maybe this concept can help. It is a naive approach; do not try to pass it a literal string.

#include <stdio.h>
#include <ctype.h>

char *sanitizer(char *src);

int main (void)
{
    char text[] = "Many spaces in between";

    printf(">>%s<<\n", sanitizer(text));

    return 0;
}

char *sanitizer(char *src)
{
    char *scout = src;
    char *group = src;

    /* walking though string */
    while (*scout) {
        if (isspace(*scout)) {
           /* space found skip ahead */
            ++scout;
        } else {
           /* coping none blank char */
            *group++ = *scout++;
        }
    }
    *group = '\0'; /* make it a string in c */
    
    return src;
}
commented: Helpful! +9
commented: Most effective and simplest solution +11

Members.

Rather then requesting user to use code-tags please use "Flag Bad Post" option to notify moderators about that post. (post missing codes or it is in wrong section)

Thanks.

commented: It would be an improvement to advertise that more often and clearly. Thanks! +9

Do you see the input example above? Well my code is obviously wrong since iam an amateur programmer (about a month now learning it) so I was wondering if someone could give me some suggestions so I can produce the expected output. I want it to be stored into a string because im going to do some manipulation with it (sorting, finding the maximum value,etc).

[B]speedy94519>[/B] Well my code is obviously wrong since iam an amateur programmer (about a month now learning it)

We all have been there. The only difference in code, between an amateur and season programmer is that instead of being "obviously wrong", is being, "not so obviously", wrong.

Concerning your particular case. I suggest you approach the issue as follow:

Obtain the line from file
Sanitize it, making it conform a pattern (eliminate blank, substitute unwanted chars, etc)
Use the pattern to extract information
Save information
Then, compare, sort, play, do whatever you want with it.
Member Avatar for iamthwee

@Aia, whilst your function probably does everything the OP desires ... may want to look at a trim() function.

http://stackoverflow.com/questions/122616/painless-way-to-trim-leadingtrailing-whitespace-in-c

I know given the OPs examples trim() is probably an overkill...

but it would be necessary if input was like

John Scott   ,  Malcom       ,P  , 1.23

Where it would fail, joining JohnScott together as one word. But that example would probably never occur for the OP!

[B]speedy94519>[/B] Well my code is obviously wrong since iam an amateur programmer (about a month now learning it)

We all have been there. The only difference in code, between an amateur and season programmer is that instead of being "obviously wrong", is being, "not so obviously", wrong.

Concerning your particular case. I suggest you approach the issue as follow:

Obtain the line from file
Sanitize it, making it conform a pattern (eliminate blank, substitute unwanted chars, etc)
Use the pattern to extract information
Save information
Then, compare, sort, play, do whatever you want with it.

Thank you for you approach towards this problem Aia ;) i really appreciate you helping me out, I actually solved it now. Thanks ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.