Jump to content

Help with searching through file for string and value

- - - - -

  • Please log in to reply
7 replies to this topic

#1
kristyy_mariee

kristyy_mariee

    Newbie

  • Members
  • Pip
  • 6 posts
A patient database for people with OCD.
I have a section of the text file in the format below for an entire month for one patient.

January 1 Monday Brushing_teeth 9
January 1 Monday Washing_hands 5
January 1 Monday Twitching 6
January 1 Monday Lock_checking 40
January 2 Tuesday Brushing_teeth 10
January 2 Tuesday Eating_hair 10
January 2 Tuesday Twitching 10
January 3 Wednesday Brushing_teeth 5
January 3 Wednesday Grouping_food 3
January 4 Thursday Brushing_teeth 6
January 4 Thursday Washing_hands 10
January 4 Thursday Washing_Face 5
January 5 Friday Brushing_teeth 4
January 5 Friday Grouping_food 4
January 6 Saturday Brushing_teeth 7

I would like to be able to search through the file and for each activity the person does, add the amount of times it is done every time it occurs, and avg per week and per month
For eg. Brushing teeth 10
.....
.... brushing teeth 6

then total would be 16 etc etc.

#include <stdio.h>

#include <string.h>




		

	

	int main()

	{

		

		int reps;

	

		char comp[15][30]={"Brushing_teeth","Washing_Hands","Eating-Hair","Scratching","Biting_Nails","Cleaning_House","Changing_clothes","Twitching","Scrubbing_Floor","Skipping_last_step","Cracking_Knuckles","Grouping_Food","Washing_Face","Lock_Checking","Biting_Inside_Of_Mouth"};

	char name[20];

	char compulsions[40];

   char month[10], day[20];

   int date;

   int i,j;

   int total=0;





printf("Welcome to Track Me\n");

printf("Below is the list of all compulsions patients have done this month\n");


printf("%s\n %s\n %s\n %s\n %s \n%s \n%s",comp[0],comp[1],comp[2],comp[3],comp[4],comp[5],comp[6]);


printf("%s \n%s\n %s\n %s\n %s \n%s \n%s\n %s",comp[7],comp[8],comp[9],comp[10],comp[11],comp[12],comp[13],comp[14]);


printf("\n\n%s",comp[6]);


//to search for patient file



printf("Enter Patients Name to access file with extension .txt");

scanf("%s",name);


FILE *search;



search = fopen(name,"r");

if (search==NULL ){

    printf("File could not be opened");

}



printf("Month  Date  Day  Compulsion  Repetitions\n");


while ( fscanf(search,"%s %d %s %s %d", month, &date, day, compulsions, &reps) > 4)

{

	printf("%s %d %s %s %d\n",month, date, day, compulsions, reps);

	if (compulsions==comp[0]){

	

				reps+=reps;

	}



        

        

		}

printf("total amount of reps: %d\n", reps);   

        

    


          

           

fclose(search);

         

	} 


#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
printf("%s\n %s\n %s\n %s\n %s \n%s \n%s",comp[0],comp[1],comp[2],comp[3],comp[4],comp[5],comp[6]);

printf("%s \n%s\n %s\n %s\n %s \n%s \n%s\n %s",comp[7],comp[8],comp[9],comp[10],comp[11],comp[12],comp[13],comp[14]);
Above code screams for loop:
for (int i = 0; i < 15; ++i)
    printf("%s \n", comp[i]);
You're also looking for (comparing) only first compulsion, not for all on the list.

What you need (not need, but it makes it super easy) is a hash map (array where index is a string, not an integer).
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
kristyy_mariee

kristyy_mariee

    Newbie

  • Members
  • Pip
  • 6 posts
Thanks for the response :)
So you're saying replace the first set of code with that for loop?
Im not familiar with a hash map, could you explain?

---------- Post added at 04:04 PM ---------- Previous post was at 03:14 PM ----------

instead of the if statement i had tried strcmp(comp[0],compulsions) and then printing the rep+=rep after, but i realized that i would need to do that for each line of the file.. I've been trying with some code to do with line numbers and then i tried to do a struct to hold the compulsion and rep but nothing really works. When i print rep at the end, i get the value for rep at the last line of file..

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Try this or this library, they are C libraries for hash table. You can read more about hashtables on wiki.

You can't use == operator to compare 2 cstrings. You have to use strcmp as you've already said. Also, rep += rep is the same rep = 2 * rep.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
I see a couple of problems:
1. reps doesn't seem to be initialized.
2. When you say "reps+=reps" you're not adding the number in the file to reps. You're doubling the value of reps. You need a second variable to scan, then add that to reps.
3. You're comparing strings with ==. Since strings are pointers, this will compare their addresses, not their values.

You haven't specified exactly what the problem is, so I don't know if that helps.

Edited by DarkLordofthePenguins, 29 November 2011 - 06:02 AM.

Programming is a journey, not a destination.

#6
kristyy_mariee

kristyy_mariee

    Newbie

  • Members
  • Pip
  • 6 posts

DarkLordofthePenguins said:

I see a couple of problems:
1. reps doesn't seem to be initialized.
2. When you say "reps+=reps" you're not adding the number in the file to reps. You're doubling the value of reps. You need a second variable to scan, then add that to reps.
3. You're comparing strings with ==. Since strings are pointers, this will compare their addresses, not their values.

You haven't specified exactly what the problem is, so I don't know if that helps.

Ok so this is the code ive changed
printf("Month  Date  Day  Compulsion  Repetitions\n");


while ( fscanf(search,"%s %d %s %s %d", month, &date, day, compulsions, &reps) > 4)

{

	printf("%s %d %s %s %d\n",month, date, day, compulsions, reps);}

	

//while(fgets(compulsions, sizeof(compulsions), search) != NULL)

//{

//fscanf(search,"%d",reps);


      if(strcmp("comp[0]",compulsions) == 0)

      {

total=total+reps;

	}



        

      printf("total amount of reps: %d\n", &total);   


        

    

	

         

           

fclose(search);

         

	} 
im gettin a number for total like 2345485 which is wrong.
To explain the problem again i basically have a text file with a list holding data like this January 1 Monday Brushing_teeth 9
January 1 Monday Washing_hands 5
January 1 Monday Twitching 6
January 1 Monday Lock_checking 40
January 2 Tuesday Brushing_teeth 10
January 2 Tuesday Eating_hair 10
January 2 Tuesday Twitching 10
January 3 Wednesday Brushing_teeth 5
January 3 Wednesday Grouping_food 3
January 4 Thursday Brushing_teeth 6
January 4 Thursday Washing_hands 10
January 4 Thursday Washing_Face 5
January 5 Friday Brushing_teeth 4
January 5 Friday Grouping_food 4

this continues for the entire month. im supposed to go through the string and whereever theres an occurrence of the same activity, add the amount of reps per occurrence.
So im trying to compare the activity in the array to the activity in the file and then add the reps, but something is missing, just dont know what.

#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
if(strcmp("comp[0]",compulsions) == 0)
Without the quotes. Also, you need to go through all entries in comp array, not just the first one.
printf("total amount of reps: %d\n", &total);
& means address in this case, and you're displaying an address, not the value.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#8
kristyy_mariee

kristyy_mariee

    Newbie

  • Members
  • Pip
  • 6 posts
Well i just did that, now i get 0.
I figured as much, i just dont know how to go through line by line . ive tried code to keep track of line numbers, and ive tried some with buffers too but nothing reallly works.

---------- Post added at 07:09 AM ---------- Previous post was at 07:06 AM ----------

Me using the first element of the array comp[0] is just a test to see if itll actually work for now.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users