question mark on save file 
Author Message
 question mark on save file

I wrote this questionaire program that prints out your answers onto a
text file. When it writes out the file name, there is a question mark
between the filename and the period. (asdfg?.txt) How do I fix it? My
source code is attached

#include <stdio.h>
#include <stdlib.h>

int tdwmode();
int txtmode();

int main()
{
        char choice [200];

        printf("\n     |||||||||||||||||||||||||||||||||||||\n");
        printf("\n     |TextDocuWiz version 1.1            |       \n");
        printf("\n     |||||||||||||||||||||||||||||||||||||\n     ");
        printf("\n");
        printf("     Licensed under the GPL \n");

        printf("     |||||||||||||||||||||||||||||||||||||\n\n");

printf("\nWhich Document type do you wish to create?/-text-/ /-tdwm-/:
");
fgets(choice, sizeof choice, stdin);
if (choice == 1)
        txtmode();
else    
        tdwmode();

Quote:
}

tdwmode()
{
char wname [1000], wtitle [1000], wauthor [1000], wthetext [31999],
wcreatedate [30];
        FILE *woutfile;
        char tdw[]=".tdw";
        printf("\n\nEnter the file name of your TextDocuWiz document:");
        fgets (wname, sizeof wname-strlen(tdw), stdin);
                if(strchr(wname,'\n')==NULL){
        int d;
        while((d=getchar())!=EOF && d!='\n')
                ;
                }
                strcat (wname,tdw);
        printf("\nEnter the title of your page: ");
        fflush(stdout);
        fgets(wtitle, sizeof wtitle-strlen(tdw), stdin);

        printf("\nWhat is the author of the document's name?: ");
        fgets(wauthor, sizeof wauthor-strlen(tdw), stdin);

        printf("\nPlease enter today's date: ");
        fgets(wcreatedate, sizeof wcreatedate-strlen(tdw), stdin);

        printf("\nPlease enter the text in your TextDocuWiz document: ");
        fgets(wthetext, sizeof wthetext-strlen(tdw), stdin);

        fprintf(stdout, "You entered: /%s", wthetext);

        woutfile = fopen (wname,"w");

        fprintf(woutfile, "Title: ///%s\n"
                "Author of Document: %s"
                "Date of Creation: %s"
                "Text of Document: %s"
                ,wtitle, wauthor, wcreatedate, wthetext);
        fclose (woutfile);

printf("\nYour document %s has been created. \n\n", wname);

return 0;

Quote:
}

txtmode()
{
char fname [300], title [300], author [500], thetext [5040],
createdate [30];
FILE *outfile;
char txt[]=".txt";

fgets (fname, sizeof fname-strlen(txt), stdin);
                if(strchr(fname,'\n')==NULL){
        int c;
        while((c=getchar())!=EOF && c!='\n')
                ;
                }
                strcat (fname,txt);

printf("\nEnter the title of your page: ");
fflush(stdout);
fgets(title, sizeof title-strlen(txt), stdin);

printf("\nWhat is the author of the document's name?: ");
fgets(author, sizeof author-strlen(txt), stdin);

printf("\nPlease enter today's date: ");
fgets(createdate, sizeof createdate-strlen(txt), stdin);

printf("\nPlease enter the text in your document: ");
fgets(thetext, sizeof thetext-strlen(txt), stdin);

fprintf(stdout, "You entered: /%s", thetext);

outfile = fopen (fname,"w");

fprintf(outfile, "Title: ///%s\n"
        "Author of Document: %s"
        "Date of Creation: %s"
        "Text of Document: %s"
        ,title, author, createdate, thetext);

fclose (outfile);

printf("\nYour document %s has been created. \n\n", fname);

return 0;

Quote:
}



Sun, 06 Jun 2004 10:59:29 GMT  
 question mark on save file

Quote:

>I wrote this questionaire program that prints out your answers onto a
>text file. When it writes out the file name, there is a question mark

which line does this printout?

Quote:
>between the filename and the period. (asdfg?.txt) How do I fix it? My
>source code is attached

trim your code down to the smallest example that demonstrates the
problem. there's so much here its not possible to see properly.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>



Mon, 07 Jun 2004 07:49:28 GMT  
 question mark on save file

Quote:

>I wrote this questionaire program that prints out your answers onto a
>text file. When it writes out the file name, there is a question mark
>between the filename and the period. (asdfg?.txt) How do I fix it? My
>source code is attached

>#include <stdio.h>
>#include <stdlib.h>

my compiler complains that you've not included string.h.

Quote:
>int tdwmode();
>int txtmode();

>int main()
>{
>    char choice [200];

>    printf("\n     |||||||||||||||||||||||||||||||||||||\n");
>    printf("\n     |TextDocuWiz version 1.1            |       \n");
>    printf("\n     |||||||||||||||||||||||||||||||||||||\n     ");
>    printf("\n");
>    printf("     Licensed under the GPL \n");

>    printf("     |||||||||||||||||||||||||||||||||||||\n\n");

>printf("\nWhich Document type do you wish to create?/-text-/ /-tdwm-/:
>");
>fgets(choice, sizeof choice, stdin);
>if (choice == 1)

choice is not an int.
This code won't compile. Next time, try pasting the real code.

Quote:
>char wname [1000], wtitle [1000], wauthor [1000], wthetext [31999],

these are not initialised and thus contain garbage. I'm never sure
that fgets will then null terminate your read-in string.....

Quote:
>fgets (fname, sizeof fname-strlen(txt), stdin);

fgets does NOT remove the terminal \n on the line. You need to do that

Quote:
>outfile = fopen (fname,"w");

you don't check for success on opening that file.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>


Mon, 07 Jun 2004 07:56:12 GMT  
 question mark on save file
* Bob Zimmerman
| I wrote this questionaire program that prints out your answers onto a
| text file. When it writes out the file name, there is a question mark
| between the filename and the period. (asdfg?.txt) How do I fix it? My
| source code is attached

| txtmode()
| {
| char fname [300], title [300], author [500], thetext [5040],
| createdate [30];
| FILE *outfile;
| char txt[]=".txt";
|
| fgets (fname, sizeof fname-strlen(txt), stdin);

Attempts to read sizeof fname - strlen(txt) - 1 (295) characters from
stdin into fname.  If a newline character is encountered before the
295th character it's written to the array as well.

|               if(strchr(fname,'\n')==NULL){
|       int c;
|       while((c=getchar())!=EOF && c!='\n')
|               ;
|               }

If strchr() returns non-NULL, you don't remove the '\n' from the buffer.
This is where your 'question mark' is coming from.

|               strcat (fname,txt);

[...]

| outfile = fopen (fname,"w");

You should check whether the call to fopen() was successful before
attempting to write to the file.

| fprintf(outfile, "Title: ///%s\n"
|       "Author of Document: %s"
|       "Date of Creation: %s"
|       "Text of Document: %s"
|       ,title, author, createdate, thetext);
|
| fclose (outfile);

You included rather a lot of source code.  It's more helpful if you post
a _small_ program that illustrates the problem you're having;  often you'll
discover the error yourself in the process of cutting down the code.

Jeremy.



Mon, 07 Jun 2004 08:22:50 GMT  
 question mark on save file
* Mark McIntyre
| I'm never sure that fgets will then null terminate your read-in
| string.....

Unless there's a read error (in which case it returns NULL),
fgets() _always_ adds the terminating null-character.

Jeremy.



Mon, 07 Jun 2004 09:19:01 GMT  
 question mark on save file

Quote:

>I wrote this questionaire program that prints out your answers onto a
>text file. When it writes out the file name, there is a question mark
>between the filename and the period. (asdfg?.txt) How do I fix it? My
>source code is attached

>#include <stdio.h>
>#include <stdlib.h>

You are missing string.h for strcat.
Quote:

>int tdwmode();
>int txtmode();

>int main()
>{
>    char choice [200];

>    printf("\n     |||||||||||||||||||||||||||||||||||||\n");
>    printf("\n     |TextDocuWiz version 1.1            |       \n");
>    printf("\n     |||||||||||||||||||||||||||||||||||||\n     ");
>    printf("\n");
>    printf("     Licensed under the GPL \n");

>    printf("     |||||||||||||||||||||||||||||||||||||\n\n");

>printf("\nWhich Document type do you wish to create?/-text-/ /-tdwm-/:
>");
>fgets(choice, sizeof choice, stdin);
>if (choice == 1)

choice will never equal 1.  choice[0] may equal '1' (depending on what
you type) which may be what you meant.  How did you ever execute
txtmode which is what you claim happened?
Quote:
>    txtmode();
>else        
>    tdwmode();
>}

>tdwmode()
>{

snip 50 lines unrelated to the question being asked.
Quote:
>}

>txtmode()
>{
>char fname [300], title [300], author [500], thetext [5040],
>createdate [30];
>FILE *outfile;
>char txt[]=".txt";

>fgets (fname, sizeof fname-strlen(txt), stdin);
>            if(strchr(fname,'\n')==NULL){

Do you think you could make your indenting any more random?  If you
are not going to make an effort to make the code readable, why do you
think anyone will take the time to try to read it?
Quote:
>    int c;
>    while((c=getchar())!=EOF && c!='\n')
>            ;
>            }
>            strcat (fname,txt);

If you typed in something less than the max amount of text, there will
be a \n at the end of your input, just before the \0.  strcat will
leave it there.  Your file name will now be "asdfg\n.txt".  Most of
the systems I work on will not support a file name containing a \n.
Quote:

snip
>outfile = fopen (fname,"w");

You need to check to see if fopen succeeded.
Quote:

>fprintf(outfile, "Title: ///%s\n"
>    "Author of Document: %s"
>    "Date of Creation: %s"
>    "Text of Document: %s"
>    ,title, author, createdate, thetext);

It doesn't hurt to check to see if fprintf succeeded either.

Quote:

>fclose (outfile);

>printf("\nYour document %s has been created. \n\n", fname);

>return 0;
>}

<<Remove the del for email>>


Mon, 07 Jun 2004 09:45:11 GMT  
 question mark on save file
Could you show me an example check if fopen succeeded?


Wed, 09 Jun 2004 13:38:08 GMT  
 question mark on save file

Quote:

> Could you show me an example check if fopen succeeded?

#include <stdio.h>
#include <stdlib.h>

int copy(char *old, char *new)
{
  int result = EXIT_SUCCESS;
  FILE *fpin = NULL;
  FILE *fpout = NULL;
  int ch = 0;

  fpin = fopen(old, "rb");

  if(fpin != NULL)
  {
    /* fopen succeeded for input file */

    fpout = fopen(new, "wb");
    if(fpout != NULL)
    {
      /* fopen succeeded for output file */

      while((ch = fgetc(fpin)) != EOF)
      {
        fputc(ch, fpout);
      }
      if(ferror(fpin))
      {
        fputs("Read error.\n", stderr);
        result = EXIT_FAILURE;
      }
      if(ferror(fpout))
      {
        fputs("Write error.\n", stderr);
        result = EXIT_FAILURE;
      }
      fclose(fpout);
    }
    else
    {
      fprintf(stderr, "Can't open %s for writing\n", new);
      result = EXIT_FAILURE;
    }
    fclose(fpin);
  }
  else
  {
    fprintf(stderr, "Can't open %s for reading\n", old);
    result = EXIT_FAILURE;
  }

  return result;

Quote:
}

int main(int argc, char **argv)
{
  int result = EXIT_SUCCESS;
  if(argc > 2)
  {
    result = copy(argv[1], argv[2]);
  }
  else
  {
    fputs("Usage: cpy oldfile newfile\n", stderr);
    result = EXIT_FAILURE;
  }

  return result;

Quote:
}

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Wed, 09 Jun 2004 14:57:19 GMT  
 question mark on save file

Quote:

> Could you show me an example check if fopen succeeded?

The checks of
    if(!fp)
and
    if(fp)
in f(), tell if fopen was successful.
The file will only be open once per run,
when and if L == 3 && n == 2.

unsigned f(unsigned L, unsigned n)
{
    unsigned A,B,C,D;
    static int OK = 1;
    FILE *fp = NULL;
    char *fn;  

    if(OK && L == 3 && n ==2){
        OK = 0;
        fn = "demo_1b.txt";
        fp = fopen(fn, "w");
        if(!fp){
            fprintf(stderr, "\nError opening %s for writing.\n", fn);
            exit(EXIT_FAILURE);
        }  
        fprintf(fp,
            "/* BEGIN %s */\n\n"
            "L = %u, n = %u;\n", fn, L, n);
        fputs(
            "for(D = L > 3 ? n : 1; D--;)\n"
            "for(C = L > 2 ? n : 1; C--;)\n"
            "for(B = L > 1 ? n : 1; B--;)\n"
            "for(A = L     ? n : 1; A--;)\n"
            "    ++n;\n\n"
            "D   C   B    A    n\n\n", fp);
    }  
    for(D = L > 3 ? n : 1; D--;)  
    for(C = L > 2 ? n : 1; C--;)
    for(B = L > 1 ? n : 1; B--;)  
    for(A = L     ? n : 1; A--;){
        ++n;
        if(fp){
            fprintf(fp,"%u   %u    %u   %u   %u\n",D,C,B,A,n);
        }
    }
    if(fp){
        fprintf(fp, "\n/* END %s */\n", fn);
        fclose(fp);
        printf("\nLook at %s\n", fn);
    }
    return n;

Quote:
}

--
 pete


Wed, 09 Jun 2004 17:01:07 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. ActiveX control stays marked dirty after save

2. question mark operator questions (forwarded)

3. Basic Open File, Save/ Save as functions for dos word processor

4. ! Saving (graphics) to file - saving window contents (PICT) in Code warrior C (on a Mac PPC)

5. How to obtain name of file on Save/Save As

6. QUESTION: saving error messages to file.

7. Question on file saving crashing system

8. question saving a file

9. simple question:how to save CBitmap to bmp file

10. Newbie Question: File Save in SDI

11. File Saving Question

12. How to obtain name of file on Save/Save As

 

 
Powered by phpBB® Forum Software