Files with strings and ints won't print to screen!

This is a discussion on Files with strings and ints won't print to screen! within the C Programming forums, part of the General Programming Boards category; I have one file kristina.txt which holds data in the format below for one person with OCD, im supposed to ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    34

    Files with strings and ints won't print to screen!

    I have one file kristina.txt which holds data in the format below for one person with OCD, im supposed to open files from names the user enters for patients with OCD and then perform calculations on the number of times they do certain repetitions.

    January 1 Monday brushed_teeth 9
    January 1 Monday combed_hair 5
    January 1 Monday stepped_up_bottom_step 20

    From the first file comp.txt is used to display all possible compulsions for patients.
    The code below prints all the text from comp.txt, but kristina.txt prints bogus numbers.
    Also the section of code where the user enters the file name to open it i turned into a comment for now because it did not work, therefore i put the actual file name for now (kristina)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        
        char compulsions[30];
       int patients=1;
       char month[10], day[10],comp[30];
       int date, reps;
    
    printf("Welcome to Track Me");
    
    FILE *ptr;
    
    // open the file ".txt" for reading
    ptr = fopen("comp.txt", "r");
    if (ptr==NULL ){
        printf("File could not be opened");
    
    }
    
    fscanf(ptr,"%s",compulsions);
    printf("List of ALL 15 Compulsions\n");
           while(!feof(ptr)){
    
    printf("%s\n",compulsions);
    fscanf(ptr,"%s",compulsions);
    
           }
           fclose(ptr);
    //system("cls");
    fflush;
    
    //to search for patient file
    
    
    //printf("Enter Patients Name to access file");
    //scanf("%s",name);
    
    FILE *search;
    
    
    search = fopen("kristina.txt","r");
    if (search==NULL ){
        printf("File could not be opened");
    
    }
    
    fscanf(search,"%s,%d, %s, %s,%d",month,&date,day,comp,&reps);
    
    
           while(!feof(search)){
    char buffer[60];
    fgets(buffer, 60, search);
    
    printf("%s, %d, %s, %s, %d",buffer);           
    fscanf(search,"%s, %d, %s, %s, %d", month, &date, &day, comp, &reps);
    
               
    
    
           }
           fclose(search);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are accessing that file 3 times for every time you print data...

    your first read at line 49 should work as you exepect if you get rid of the commas inside the quotes...
    fscanf(search," %s %d %s %s %d", month, &date, day, comp, &reps);
    You need to lose lines 52 to 55 and 57... and fix your printf...
    So your final print out should look like this...
    Code:
    while ( fscanf(search," %s %d %s %s %d", month, &date, day, comp, &reps) > 3)
      printf("%s %d %s %s %d\n",month, date, dat, comp, reps);
    
    fclose(search);

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    Thank you it works, but for some reasons its skipping the first line and only printing the last 2 lines from the file and not the 3 lines

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Kristyy_mariee View Post
    Thank you it works, but for some reasons its skipping the first line and only printing the last 2 lines from the file and not the 3 lines
    You've still got line 49 in there don't you?
    Lose it.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    oh yeah i just took it out, works now. So, I just tried to read in name and then use that as the file name instead, but that doesnt work?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Post your current code...

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    Code:
    #include <stdio.h>
    
    int main()
    {
        
        char compulsions[30];
        char name[20];
       int patients=1;
       char month[10], day[10],comp[30];
       int date, reps;
    
    printf("Welcome to Track Me");
    
    
    // open the text file "comp.txt" for writing
    FILE *ptr;
    
    // open the file ".txt" for reading
    ptr = fopen("comp.txt", "r");
    if (ptr==NULL ){
        printf("File could not be opened");
    
    }
    
    fscanf(ptr,"%s",compulsions);
    printf("List of ALL 15 Compulsions\n");
           while(!feof(ptr)){
    
    printf("%s\n",compulsions);
    fscanf(ptr,"%s",compulsions);
    
           }
           fclose(ptr);
    
    
    fflush;
    //to search for patient file
    
    //for (patients=1; patients <=15; patients++)
    //{
    
    //printf("Enter Patients Name to access file");
    //scanf("%s",&name);
    
    FILE *search;
    
    
    search = fopen("kristina.txt","r");
    if (search==NULL ){
        printf("File could not be opened");
    
    }
    
    //fscanf(search,"%s %d %s %s %d",month,&date,day,comp,&reps);
    
    while ( fscanf(search,"%s %d %s %s %d", month, &date, day, comp, &reps) > 4)
    
      printf("%s %d %s %s %d\n",month, date, day, comp, reps);
     
    
    fclose(search);
           
    
    
    
               
    
    
           
           }

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    IL
    Posts
    7
    You still have fopen hard coded...
    Last edited by xxc0dephr34kxx; 11-26-2011 at 11:35 AM. Reason: correction

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    That would cause what prob?

  10. #10
    Registered User
    Join Date
    Nov 2011
    Location
    IL
    Posts
    7
    Your wanting to use name[20] as a file name correct? If so what is wrong here?

    Code:
    ptr = fopen("comp.txt", "r");
    
    search = fopen("kristina.txt","r");

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    what the user enters would be stored as the string name, then i'd replace "kristina.txt" with name , but when i do that the file does not open.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Did you type .txt at the end?

  13. #13
    Registered User
    Join Date
    Nov 2011
    Location
    IL
    Posts
    7
    Code:
    FILE *ptr;
     
    // open the file ".txt" for reading
    ptr = fopen("comp.txt", "r");
    Looking at your code, I don't see where you are asking the user to input a file name, and I don't see where that input is being stored int name[20]. Your file names are still hardcoded.

    it should be something like

    Code:
    ptr=fopen(an_array_name, "r");

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    No i dont

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    The section of code that i commented out. I prompted the user to enter a file name
    printf("Enter Patients Name to access file");

    scanf("%s",&name);

    If it WORKED i wouldve therefore put name when i go to open the file instead of having kristina.txt

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting ints to strings?
    By rodrigorules in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2009, 04:49 AM
  2. Array of Ints to a Strings
    By mattAU in forum C Programming
    Replies: 10
    Last Post: 08-17-2006, 06:25 AM
  3. Strings into Ints
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2002, 05:07 PM
  4. converting strings to ints
    By HomerJ in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 07:08 PM
  5. Converting strings to ints
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2002, 03:48 PM

Tags for this Thread


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