Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,258 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,185 people online right now. Registration is fast and FREE... Join Now!




To print a sentence from a file in C with spaces & all

 
Reply to this topicStart new topic

To print a sentence from a file in C with spaces & all

c_sen28
post 27 Aug, 2008 - 07:29 AM
Post #1


New D.I.C Head

*
Joined: 27 Aug, 2008
Posts: 14


My Contributions


Hi,
I want my program to print sentences for me from a file. I
CODE
t is doing that but without the spaces in between. What's going wrong ? The code is given below & the input & output files too.

Code

[code]
#include<stdio.h>

main()
{
FILE *ifp, *ofp;
char *mode = "r";
char outputFilename[] = "textout";

ifp = fopen("text", mode);

if (ifp == NULL) {
  fprintf(stderr, "Can't open input file in.list!\n");
  exit(1);
}

ofp = fopen(outputFilename, "w");

if (ofp == NULL) {
  fprintf(stderr, "Can't open output file %s!\n",
          outputFilename);
  exit(1);
}

char sentence[1000];

while (fscanf(ifp, "%s", sentence) != EOF){
  fprintf(ofp, "%s", sentence);
}

fclose(ifp);
fclose(ofp);
}


Input file:

1. The keeper keeps the keep in the night.

2. The keeper sleeps in the night.


Output file:

1.Thekeeperkeepsthekeepinthenight.2.Thekeepersleepsinthenight.

This post has been edited by c_sen28: 27 Aug, 2008 - 07:42 AM
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 27 Aug, 2008 - 07:42 AM
Post #2


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 502



Thanked 44 times

Dream Kudos: 25
My Contributions


Two ways to do this:

1. Instead of fscanf use fgets to get the entire line.
2. Use a format specifier in fscanf to read everything not a newline:
c
fscanf(ifp, "%[^\n]s", sentence);
User is offlineProfile CardPM

Go to the top of the page

c_sen28
post 27 Aug, 2008 - 08:38 AM
Post #3


New D.I.C Head

*
Joined: 27 Aug, 2008
Posts: 14


My Contributions


The format specifier thing is not working. The output file size exceeds the limit. Why's that ? what else can be done ? Not much aware abt the fgets fn.




QUOTE(JackOfAllTrades @ 27 Aug, 2008 - 08:42 AM) *

Two ways to do this:

1. Instead of fscanf use fgets to get the entire line.
2. Use a format specifier in fscanf to read everything not a newline:
c
fscanf(ifp, "%[^\n]s", sentence);


User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 27 Aug, 2008 - 09:28 AM
Post #4


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 502



Thanked 44 times

Dream Kudos: 25
My Contributions


QUOTE(c_sen28 @ 27 Aug, 2008 - 12:38 PM) *

Not much aware abt the fgets fn.


Which is why I linked to a page which explains how it works, with an example and everything.

And please...use complete, proper English words on Internet forums, not "text message speak." This is all about clear, effective, unambiguous communication.
User is offlineProfile CardPM

Go to the top of the page

c_sen28
post 29 Aug, 2008 - 05:06 AM
Post #5


New D.I.C Head

*
Joined: 27 Aug, 2008
Posts: 14


My Contributions


Thanks for your reply & sorry about using "text message speak".

Well, I tried using fgets too but the system takes an interminably long time to write just two sentences & all other programs hang up !!!

Any solution to this ?


QUOTE(JackOfAllTrades @ 27 Aug, 2008 - 10:28 AM) *

QUOTE(c_sen28 @ 27 Aug, 2008 - 12:38 PM) *

Not much aware abt the fgets fn.


Which is why I linked to a page which explains how it works, with an example and everything.

And please...use complete, proper English words on Internet forums, not "text message speak." This is all about clear, effective, unambiguous communication.

User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 29 Aug, 2008 - 07:43 AM
Post #6


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 502



Thanked 44 times

Dream Kudos: 25
My Contributions


Are you calling fgets properly, like this?
c
while (NULL != fgets(sentence, sizeof(sentence), ifp)) 
{
//...
}
User is offlineProfile CardPM

Go to the top of the page

c_sen28
post 29 Aug, 2008 - 08:17 AM
Post #7


New D.I.C Head

*
Joined: 27 Aug, 2008
Posts: 14


My Contributions


Yes, I think so. This is my code now:

CODE


#include<stdio.h>

main()
{
FILE *ifp, *ofp;
char *mode = "r";
char outputFilename[] = "textout";

ifp = fopen("text", mode);

if (ifp == NULL) {
  fprintf(stderr, "Can't open input file text!\n");
  exit(1);
}

ofp = fopen(outputFilename, "w");

if (ofp == NULL) {
  fprintf(stderr, "Can't open output file %s!\n",
          outputFilename);
  exit(1);
}

char sentence[1000];

while (fgets(sentence, 1000, *ifp) != EOF){
  fputs(sentence, *ofp);
}

fclose(ifp);
fclose(ofp);
}



QUOTE(JackOfAllTrades @ 29 Aug, 2008 - 08:43 AM) *

Are you calling fgets properly, like this?
c
while (NULL != fgets(sentence, sizeof(sentence), ifp)) 
{
//...
}


User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 29 Aug, 2008 - 09:24 AM
Post #8


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 502



Thanked 44 times

Dream Kudos: 25
My Contributions


No, that's not right. Read what I wrote, and compare to yours.

You should also NOT be using *ifp and *ofp.
User is offlineProfile CardPM

Go to the top of the page

c_sen28
post 30 Aug, 2008 - 05:48 AM
Post #9


New D.I.C Head

*
Joined: 27 Aug, 2008
Posts: 14


My Contributions


Thanks a lot. It works perfectly now !!! smile.gif



This post has been edited by c_sen28: 30 Aug, 2008 - 05:48 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 11:02PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month