Thread: File not printing

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231

    File not printing

    Hello again. So I was making a program that makes a file but its not making it for some reason. I can not for the life of me figure out this most likely painfully obvious problem so please help me (This is a study program and finals are next week).

    The BLU (TF2 reference) is the problem

    Code:
    #include <stdio.h>
    
    int testmkr();
    
    main()
    {
          printf("Hello, would you like to make a test or take one.\n\
          \npress '1' to make a test\nor '2' to take a test");
          
          if(getch() == '1')
          {
                   system("cls");
                   printf("Loading test maker...");
                   testmkr();
          }
          
          
          
          
          
          
    }
    
    testmkr()
    {
             
             char title[200];
            FILE *tfile = NULL;
             
             char titlefile[200];
             
             system("cls");
             printf("...Loading complete");
             system("cls");
             
             
             
             printf("please enter a title for your test: ");
             
           
    
            puts("Enter file to be opened: ");
            fgets(titlefile, 200, stdin);
            
            strcpy(title,"C:\\Program Files\\tester\\");
    
            strcat(title,titlefile);
    
            printf("%s",title);
            
           
            tfile = fopen(title, "w"); //problem here
             
             getch();
    
             return 0;
    }
    Thank You

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are creating files that have a carriage-return (\n character) at the end. You should strip that off from the string you read with fgets (see the FAQ) before putting it in your filename. For instance, if you did
    Code:
    printf(">%s<", title);
    towards the end of your code instead of the print statement you have, you'd see the < on the next line.

  3. #3
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    Thanks, but would I use fflush or not because a lot of people hate that function.

  4. #4
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    I changed it to scanf but now I cant find the file in the destination folder.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I would create a new filename with fgets too but you need to follow this FAQ. In particular you need to use strchr to find the \n character, and then overwrite it.

    People hate fflush(stdin); because that call is undefined behavior, as explained here.

    Our FAQ is a wonderful document, as is the FAQ board, really.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Look harder.

    (Do the newer windows get upset about people trying to write to Program Files? It works for me on XP.)

  7. #7
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    I have changes my code around a little but still have 1 question.

    The variable titlefile is a pointer

    Code:
    strcat(title,titlefile);
    is there another way around this?

    thanks again

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by xniinja View Post
    I have changes my code around a little but still have 1 question.

    The variable titlefile is a pointer

    Code:
    strcat(title,titlefile);
    is there another way around this?

    thanks again
    As opposed to what? As long as it's a pointer to valid memory, everyone will be happy.

  9. #9
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    it doesn't append the line for some reason.

    the output is...

    C:\Program Files\tester\.txt
    as opposed to...

    C:\Program Files\tester\filename.txt

    whole code just in case...


    Code:
    #include <stdio.h>
    
    int testmkr();
    
    main()
    {
          printf("Hello, would you like to make a test or take one.\n\
          \npress '1' to make a test\nor '2' to take a test");
          
          if(getch() == '1')
          {
                   system("cls");
                   printf("Loading test maker...");
                   testmkr();
          }
          
          
          
          
          
          
    }
    
    testmkr()
    {
             
             char title[200];
            FILE *tfile = NULL;
             char buf[BUFSIZ];
             char *titlefile;
             
             system("cls");
             printf("...Loading complete");
             
    
            system("cls");
           
            puts("\n\nEnter title of test: ");
       
       
       
            if (fgets(buf, sizeof(buf), stdin) != NULL)
            {
            
            if ((titlefile = strchr(buf, '\n')) != NULL)
            
            *titlefile = '\0';
            
            
            }
            
            strcpy(title,"C:\\Program Files\\tester\\");
    
            printf("\nfolder: %s",title);
            
            strcat(title,titlefile);
    
            printf("\nfolder with file name: %s",title);
            
            strcat(title,".txt");
            
            printf("\n>%s<", title);
           
            tfile = fopen(title, "w");
             
            
            
            if(tfile == NULL)
            {
                     printf("Error: file was not made");
            }
            else
            {
                fprintf(tfile,"testing");
            }
            
            
            fclose(tfile);
             
             
             
             
             getch();
    }

    Im I erasing it or something please help. Thank you

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The only thing you do with titlefile is set it to the end-of-string marker. Consequently, there is no string there. Your string is in buf, so that's what you should strcat instead.

  11. #11
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    Wow, I feel like a dumbass and rejoicing at the same time. Thank you, lets see if it wrote to the file... no, can you test the code and see if it writes a file for you because its not giving me anything when i try to print it outside of the folder the program is in.

    Thanks again.

    EDIT: It does print to the folder the program is in so I'm going to go on with that, but can you please help me still.
    thank you

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If the folder C:\Program Files\tester does not exist, then you cannot create a file inside it (i.e., the system won't create the directory on its own just for you to put the file in).

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    Look harder.

    (Do the newer windows get upset about people trying to write to Program Files? It works for me on XP.)
    And on anything newer than XP it is a REAL BAD idea to put data files into the Program Files heirachy. Win7 will redirect it to a ghost folder under <username>\AppData\Roaming.

    Microsoft "security" strikes again!

  14. #14
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    Ok thanks I will see if that is happening when i get home. But does this happen on vista? Im using 7 at my dads work but my laptop has vista.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by xniinja View Post
    Ok thanks I will see if that is happening when i get home. But does this happen on vista? Im using 7 at my dads work but my laptop has vista.
    As far as I know Vista does it to...

    The thing is to make a folder someplace in your named folder to put these things... Something like:

    c:\users\<your account>\programming

    The windows native filing system is one of the very best organizational tools you can possibly use... I'm constantly amazed at the junk I find in very weird places ... movies in Windows\System32, and such... all because people don't bother to learn about the native filesystems.

    I still grin when I think about the system I set up for one of my clients. When I showed him how I'd organized it for him he asked me "What program does that"... I answered "Windows"...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM