Thread: Question About Files

  1. #1
    Registered User Actor's Avatar
    Join Date
    Jul 2008
    Posts
    7

    Lightbulb Question About Files

    Hello everyone!
    I have a question about writing a string to a file. When I try to pass a string as an input to a file, there always occurs a "new line" and I'm not able to destroy it; with this, I get a wrong counted number of words/sentences in a file. My question is how can I destroy that new line?
    Thanks in advance and happy to join this community.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Does something like this create a newline?

    Code:
    fprintf(somefile, "Some line without a newline");

  3. #3
    Registered User Actor's Avatar
    Join Date
    Jul 2008
    Posts
    7
    Yes.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	FILE *f = fopen("bleh.txt", "w");
    	if(f != NULL)
    	{
    		fprintf(f, "Line without a newline.");
    		
    		fclose(f);
    		return 0;
    	}
    	return 1;
    }
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int c;
    	FILE *f = fopen("bleh.txt", "rb");
    	
    	if(f != NULL)
    	{
    		while((c = fgetc(f)) != EOF)
    		{
    			printf("[%c] [%d]\n", c, c);
    		}
    		fclose(f);
    		return 0;
    	}
    	return 1;
    }
    Output of the 2nd program after running the first:

    Code:
    [L] [76]
    [i] [105]
    [n] [110]
    [e] [101]
    [ ] [32]
    [w] [119]
    [i] [105]
    [t] [116]
    [h] [104]
    [o] [111]
    [u] [117]
    [t] [116]
    [ ] [32]
    [a] [97]
    [ ] [32]
    [n] [110]
    [e] [101]
    [w] [119]
    [l] [108]
    [i] [105]
    [n] [110]
    [e] [101]
    [.] [46]
    I don't see any newline chars there.

  5. #5
    Registered User Actor's Avatar
    Join Date
    Jul 2008
    Posts
    7
    Sorry about my yes.
    However, with these codes I always get a new line whatever I do:
    Code:
    void Function_2(FILE *fPtr, char *Cont, int a, char *Nick)
    {
         char StringtoStore[1000000];
         int answer, i=0;
         char ch;
         fPtr = fopen(Nick, "r");
         printf("Please enter a string to store in the file &#37;s\n", Nick);
         if(fgets(Cont, a, fPtr) != NULL)
         {          
                 printf("The file contains data!!! Do you want to overwrite?(1 for YES/0 for NO): ");
                 scanf("%d", &answer);
                 if(answer == 1)
                 {
                           fPtr = fopen(Nick, "w");
                           while ((ch = getchar()) != EOF)
                           {
                                    StringtoStore[i]=ch;
                                    i++;
                           }
                           StringtoStore[i]='\0';
                           fputs(StringtoStore, fPtr);
                           fclose(fPtr);
                 }
                 else
                 {
                           printf("No overwriting will be executed");
                           fclose(fPtr);
                 }
         }
         else
         {
                 fPtr = fopen(Nick, "w");
                 while ((ch = getchar()) != EOF)
                 {
                       StringtoStore[i]=ch;
                       i++;
                 }
                 StringtoStore[i]='\0';
                 fputs(StringtoStore, fPtr);
                 fclose(fPtr);
         }
    }

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    fputs() automatically appends a newline. Don't use it if you don't want a newline. Use something like fprintf().

  7. #7
    Registered User Actor's Avatar
    Join Date
    Jul 2008
    Posts
    7
    Thanks for your response
    But how can I write the whole string into a file using fprintf?

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    This:

    Code:
    fputs(StringtoStore, fPtr);
    Would become something like this:

    Code:
    fprintf(fPtr, "&#37;s", StringtoStore);

  9. #9
    Registered User Actor's Avatar
    Join Date
    Jul 2008
    Posts
    7
    Thank you so much MacGyver! But I still get a new line The program also says that there's data in the file altough there is nothing in it :S
    Last edited by Actor; 07-02-2008 at 05:09 PM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Actor View Post
    Hello everyone!
    I have a question about writing a string to a file. When I try to pass a string as an input to a file, there always occurs a "new line" and I'm not able to destroy it; with this, I get a wrong counted number of words/sentences in a file. My question is how can I destroy that new line?
    Thanks in advance and happy to join this community.
    The fprintf() function doesn't add a newline char, so if you have a newline, then the newline was a part of your string.

    The simple answer is not to "destroy" the newline char, it's to fix your logic of counting the words/sentences, so that the newline is not a problem.

    Just treat the newline char like you would another space between letters, or like a comma. It doesn't increase the count of words, and it doesn't increase the count of sentences. It just is a nothing for you program's logic to handle, as far as counting goes.

    And Welcome to the Forum!

  11. #11
    Registered User Actor's Avatar
    Join Date
    Jul 2008
    Posts
    7
    It's not a part of my string actually, as you can see it in the codes too.
    My program sees my file never empty even though I erase all the data in it :S That's what I've noticed and don't know the reason.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Actor View Post
    It's not a part of my string actually, as you can see it in the codes too.
    My program sees my file never empty even though I erase all the data in it :S That's what I've noticed and don't know the reason.
    Look again at your computer - there is no eraser on it.

    You can over-write data, but you can't "erase" it. Files consist of other info, which lists it's name, and it's several attributes, as well. This is all part of the file "header".

    You can "erase" all you want - the file header will remain until the file is over-written, whether the file has data in it, or not. A file with a size of zero bytes, still has a header. After the file has been marked as "deleted", then the file header will still exist, but the first byte of the file header will mark it as "deleted", and you won't see it listed in your list of files.

    For the life of me, I have no idea why and what you're trying to do with newlines. Are you discriminating against them, kind sir?
    Last edited by Adak; 07-02-2008 at 06:28 PM.

  13. #13
    Registered User Actor's Avatar
    Join Date
    Jul 2008
    Posts
    7
    Thanks for the response.

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're probably storing a newline in StringtoStore when the user presses enter at the commandline. Check for '\n' as well as EOF in your while condition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question on reading files in C
    By TaiL in forum C Programming
    Replies: 12
    Last Post: 10-05-2008, 09:48 PM
  2. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  3. input/output files question
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2005, 12:38 PM
  4. Question about ".o" files
    By Jez_Master in forum C++ Programming
    Replies: 1
    Last Post: 04-11-2002, 01:54 AM
  5. Using files: Wacked Question
    By Denethor2000 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2002, 12:54 AM