hmm, question!

This is a discussion on hmm, question! within the C Programming forums, part of the General Programming Boards category; I wrote this K-Journal program out of complete boredom, when I compile it under C++ Specs it works, when I ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    hmm, question!

    I wrote this K-Journal program out of complete boredom, when I compile it under C++ Specs it works, when I compile under C it does not work, can someone explain why and if possible explain what I should do so I can compile it under C
    Code:
    /*
     * k-journal.c
     * A console based Journal
     
        Copyright (C) 2002  Lucas Campbell
    
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.
    
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
    
        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
     */
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    int main()
    {
    	char str[9999], retime[25];
    	time_t tp;
    	tp = time(NULL);
    	printf("	 _       _\n");
    	printf("	| |    / /\n");
    	printf("	| |   / /\n");
    	printf("	| |  / /\n");
    	printf("	| | / /\n");
    	printf("	|  ^ /    JOURNAL\n");
    	printf("	| |^ \\\n");
    	printf("	| |  \\ \\\n");
    	printf("	| |    \\ \\\n");
    	printf("	|_|     \\_\\\n");
    	printf("+---------------------------+\n");
    	printf("Enter your text: ");
    	FILE *fp = fopen("k-journal.txt","a");
    	gets(str);
    	if(!strcmp("l",str))
    		fprintf(fp,"Blank (l)");
    	else
    		fprintf(fp,str);
    	fprintf(fp,"\n");
    	strcpy(retime,ctime(&tp));
    	fprintf(fp,retime);
    	printf("%s\n\n ",retime);
    	fclose(fp);
    	return 0;
    }
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    /*
     * k-journal.c
     * A console based Journal
     
        Copyright (C) 2002  Lucas Campbell
    
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.
    
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
    
        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
     */
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    int main()
    {
    	char str[9999], retime[25];
    	time_t tp;
    	FILE *fp;
    	tp = time(NULL);
    	printf("	 _       _\n");
    	printf("	| |    / /\n");
    	printf("	| |   / /\n");
    	printf("	| |  / /\n");
    	printf("	| | / /\n");
    	printf("	|  ^ /    JOURNAL\n");
    	printf("	| |^ \\\n");
    	printf("	| |  \\ \\\n");
    	printf("	| |    \\ \\\n");
    	printf("	|_|     \\_\\\n");
    	printf("+---------------------------+\n");
    	printf("Enter your text: ");
    	fp = fopen("k-journal.txt","a");
    	gets(str);
    	if(!strcmp("l",str))
    		fprintf(fp,"Blank (l)");
    	else
    		fprintf(fp,str);
    	fprintf(fp,"\n");
    	strcpy(retime,ctime(&tp));
    	fprintf(fp,retime);
    	printf("%s\n\n ",retime);
    	fclose(fp);
    	return 0;
    }
    This works. It was your file pointer. I think...

    File *fp; first

    THEN..

    fp = fopen....

    That could be one reason.
    The world is waiting. I must leave you now.

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Explanation:
    File pointer assignments always come first. Later on, you use the actual pointer to do file work.

    Beinning of code:
    FILE * MyFileToUse;

    Later on in code:
    MyFileToUse = fopen("c:\\foo.txt", "attributes");
    fprintf(MyFileToUse, "blahblahblah\n");
    fclose(MyFileToUse);

    - edit -
    In c++ you can assign them anywhere. In C you have to put them at the beginning of your function. I learned that the hard way.
    Last edited by Shadow; 05-10-2002 at 12:11 AM.
    The world is waiting. I must leave you now.

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    thanks!
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  3. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  4. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM
  5. Math Question, hard
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 12-08-2001, 10:58 AM

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