Thread: File Writing

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    17

    File Writing

    I keep trying to write actual text to a .txt file. I have the file created but I am unable to actually take the user inputted string and put it into this text file. I have looked over multiple guides including the intro to C on here but still not quite there. I'm not getting any build errors either. Anyone know? Thanks in advance

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define X 200
    
    /*
     * 
     */
    
    struct student{
    
        char name[25];
        int assignments[6];
        int exams[2];
        int ID;
        double grade;
    
    
    } students[X];
    
    int main() {
        
        FILE *grade;
       
        int x = 0;
        int y = 0;
        int a;
        
        char string[25]; 
        char fname[20];
    
        printf("Input file name: ");
    
        scanf("%s", fname);
        
        grade = fopen(fname, "w");
        
        printf("How many students do you have: ");
        
        scanf("%d", &x);
    
        for(; y <= x ; y++){
            
            printf("\nEnter student name: ");
            fscanf(stdin, "%s", string);
            
            strcpy(students[y].name, string);
            
            fprintf(stdout, "%s ", students[y].name);
            
        }
        
        fclose(grade);
    
        return 0;
        
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Perhaps
    fprintf(grade, "%s ", students[y].name);
    at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    17
    Isn't that just for printing from the file? Also I thought thats what " fprintf(stdout, "%s", students[y].name); " did? At least thats what I have gotten out of much of what I've looked over online, but this is my first go around with files =/

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    No, it prints TO a file.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    17
    Ahhhh! Lol I did not know that. I was getting seriously messed up here I kept saying "the fscanf's are right there!" Thank you very much. If you could clarify by any chance, whats the point in getchar and putchar? Single characters?

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Yes, those are for single characters.

    There are (perhaps not so) subtle differences between using scanf and getchar for reading a single character - having to with needing (or not) to hit "enter", and/or in leaving behind a character in the input buffer that one generally needs to purge.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int y = 0;
    
    ...
    
    printf("How many students do you have: ");
        
    scanf("%d", &x);
    
    for(; y <= x ; y++){
    With y initialized to zero, this goes through the loop one too many times. You need y < x instead. Further, there is no bound checking on the number of students the user wishes to enter data for. If you ask the question "how many do you want" and the user enters 500, then your loop will happily go about its job asking data for 500 students and trying to assign data to elements of the students array that do not exist. For that, you'll either need to check the user's entered input and issue an error message and reprompt for a valid number or you can simply ignore any value the user types that is above 200. A possible way to do that would be:
    Code:
    for(; y < x && y < X; y++ )
    That then brings up a potential source of confusion where you have x and X which are way too close looking for my tastes... you should strive to pick better variable/identifier names.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    17
    Oh okay, well thanks again for the help. I'm sure I'll be back with more stupid questions (:

    I love how as I post "with more stupid questions" your sig says "There no stupid questions, only stupid people"

    ...how upsetting

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    That part of my sig was there way before you ever came along to this boards. It is simply a reflection of my overall pessimistic views on our fellow man and is not meant to apply to you specifically.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    17
    uh oh I am in trouble now. So I expanded my program and I am attempting to add 6 assignment grades with each of my lovely little students. Although after I input the initial grade, the run ends, it does say run failed but I don't see why? Perhaps better eyes would spot it.

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define X 200
    
    /*
     * 
     */
    
    struct student{
    
        char name[25];
        int assignments[6];
        int exams[2];
        int ID;
        double grade;
    
    
    } students[X];
    
    int main() {
        
        FILE *grade;
       
        int x = 0;
        int y = 0;
        int a;
        int z = 0;
        
        char string[25]; 
        char fname[20];
    
        printf("Input file name: ");
    
        scanf("%s", fname);
        
        grade = fopen(fname, "w");
        
        printf("How many students do you have: ");
        
        scanf("%d", &x);
    
        for(; y < x ; y++){
            
            printf("Enter student name: ");
            scanf("%s", string);
            
            strcpy(students[y].name, string);
            //students[y].ID = a;
            //a++;
            
            fprintf(grade, "%s\n", students[y].name);
            //printf("%d\n", students[y].ID);
            
        for(; z < sizeof(students[y].assignments[z]) ; z++ ) {
            
            printf("Hello");
    
            printf("Enter their 6 assignment grades: ");
            scanf("%d", students[y].assignments[z]);
            
            printf("world"); //DOESNT GET THIS FAR
            
            fprintf(grade, "%d", students[y].assignments[z]);
            
        }
            
        }
       
        fclose(grade);
    
        return 0;
        
    }

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    for(; z < sizeof(students[y].assignments[z]) ; z++ ) {
    Please explain why you think the above for loop is right?
    Why do you think you can run it inside another for loop? (How does z get reset?)
    What do you think the value of "sizeof(students[y].assignments[z])" is? (I do not think it is what you guess it is; sizeof is normally done at compile time NOT runtime)

    Tim S.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    17
    Quote Originally Posted by stahta01 View Post
    Code:
    for(; z < sizeof(students[y].assignments[z]) ; z++ ) {
    Please explain why you think the above for loop is right?
    Why do you think you can run it inside another for loop? (How does z get reset?)
    What do you think the value of "sizeof(students[y].assignments[z])" is? (I do not think it is what you guess it is; sizeof is normally done at compile time NOT runtime)

    Tim S.
    Well I want to traverse the assignment array and add all the grades. First by giving the student a name than it would go into this loop and start with position one of his assignments, finally go through his assignments and going back to the original loop where I would name the next student. The only thing I personally think is off is the "sizeof(...)" part. Also I'm fairly certain that "sizeof(...)" refers to the size of the assignment array.


    With that line of code I am only able to input 4 assignment grades...in case that helps any.
    Last edited by Rumpus; 05-09-2011 at 12:23 PM.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Also I'm fairly certain that
    Famous last words. ;-)

    Code:
    scanf("%d", students[y].assignments[z]);
    The above should generate a warning that you should not ignore.


    Code:
        for(; z < sizeof(students[y].assignments[z]) ; z++ )
    Not sure why you wouldn't initialize 'z' in the loop parameters rather than at the top. Makes it so much easier to read later.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    With that line of code I am only able to input 4 assignment grades...in case that helps any.
    Probably be easier to debug if you used variable names that meant something (something other than x, y, z)

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Have you checked the value returned by "sizeof(students[y].assignments[z]" ?

    I'll bet, NO.

    (this was asked earlier)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 04-20-2009, 04:33 PM
  2. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  3. writing to file
    By Mr.Pink in forum C++ Programming
    Replies: 6
    Last Post: 02-17-2005, 04:33 PM
  4. reading from file and writing in a nother file
    By undisputed007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2004, 02:17 PM
  5. Writing to File?
    By mvision in forum C Programming
    Replies: 8
    Last Post: 11-27-2001, 09:30 PM