1

I have a text file with some sentences I would like to output onto the screen.

The time to come.
Normal, common, or expected.
A special set of clothes worn by all the members of a particular group or organization
Already made use of, as in a used car.
Bing
A circle of light shown around or above the head of a holy person.
The god of thunder.
An act that is against the law.
Long dress worn by women.
Odd behaviour.

This is the code I have used to make the output for the Words to these definitions, But Scanf doesn't like spaces, so can someone edit this code to output the definitons above, thanks.

Should of said this eariler but the output should be 1 sentence at one time.

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

FILE *fp;
int main(void)
{

struct store
{
char id[128];
}stock[10];

int printnum;
int allrec=0;
int startrec=0;

fp=fopen("Test 24 Definitions.txt","r");
printf("i");
fscanf(fp,"%s",&stock[startrec].id);

while(!feof(fp))
{
printf("%s", stock[startrec].id);
printf(" \n");
getch();
startrec=startrec+1;
allrec=startrec;
fscanf(fp,"%s",&stock[startrec].id);
}

fclose(fp); 

printf("\n\n\n\n");
int i;
for (i=0; i<allrec; i++)
{
printf("%s\n",stock[i].id);
getch();
}
}

Sample code with fgets would be appreciated

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
user3227362
  • 69
  • 1
  • 2
  • 8
  • 1
    So you know about [fgets](http://linux.die.net/man/3/fgets), have you tried something with it? – A4L Jan 23 '14 at 10:52
  • find the file size using `stat` or any other method, then read the file using `fgets`. don't use `feof`. – sujin Jan 23 '14 at 11:00
  • 1
    Please do us (and yourself) a favor and format your code correctly – Jabberwocky Jan 23 '14 at 11:08
  • 1
    Apparently `fgets()` documentation is somehow cryptic. [See this related (albeit deleted) question](http://stackoverflow.com/questions/21307408/c-programming-fscanf-output-to-fgets-output). Putting as much effort into research and solving the problem as has been put posting duplicate questions would likely have this already solved. [What have you tried?](http://mattgemmell.com/what-have-you-tried/) – WhozCraig Jan 23 '14 at 15:58

4 Answers4

0

This might help u understand

#include <stdio.h>
#include <stdlib.h>
FILE *fp;
int main(void)
{

struct store
{
char id[128];
}stock[10];

int printnum;
int allrec=0;
int startrec=0;

fp=fopen("Test 24 Definitions.txt","r");

while(!feof(fp))
{
    fscanf(fp,"%[^\t]s",stock[startrec].id);
    printf("%s", stock[startrec].id); 
}

fclose(fp);
return 0;
}
Ansh David
  • 654
  • 1
  • 10
  • 26
  • Ssshhhh! Don't shout. –  Jan 23 '14 at 11:11
  • Also, this might not help OP to understand anything, as you haven't explained the code. Furthermore, `while(!feof(f))` is always wrong. –  Jan 23 '14 at 11:12
0

Get the size of the file using ftell. Then read the file content using fgets. Don't use feof to find the end of the file. “while( !feof( file ) )” is always wrong.

Try this code.

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

#define MAX_LINE_LENGH 255

int main(void)
{
    char id[MAX_LINE_LENGH];
    int size;
    FILE *fp;
    fp=fopen("test.txt","r");

    fseek(fp, 0, SEEK_END); 
    size = ftell(fp); 
    fseek(fp, 0, SEEK_SET); 

    while(size>0)
    {
        fgets(id, MAX_LINE_LENGH, fp);
        printf("%s", id);
        /* copy this id to any char array if you want */
        size = size-strlen(id);
    }   
    fclose(fp); 
    printf("\n");
}
Community
  • 1
  • 1
sujin
  • 2,813
  • 2
  • 21
  • 33
0

Some remarks before the code:

  • I removed conio - not used or needed
  • I removed allrec since you can just use startrec which I renamed to rec
  • There's no need to use getc() (not getch(), that's from libcurses), fgets reads till the newline, including that one
  • I check if the file is actually opened or you're gonna risk reading from NULL, resulting in a segfault
  • In the printing for-loop, I use getchar() instead of getch()
#include <stdio.h>
#include <stdlib.h>
FILE *fp;
int main(void)
{

    struct store
    {
        char id[128];
    } stock[10];

    int allrec=0;
    int rec=0;
    int res;
    fp = fopen("text.txt","r");
    if(!fp) printf("failed to open file\n");

    while(!feof(fp))
    {
        res = fgets(&stock[rec].id, 128, fp);
        if(!res) {
            break;
        }
        printf("%s", stock[rec].id);
        rec++;
    }
    fclose(fp);

    printf("\n\n\n\n");
    int i;
    fflush(stdin);
    for (i=0; i<rec; i++)
    {
        printf("%s",stock[i].id);
        getchar();
    }
}
Jasper
  • 158
  • 7
0

To read a line of text using fscanf() rather than words, for this code in 2 places use:

fscanf(fp,"%127[^\n]%*c", stock[startrec].id);

"%127[^\n]" Without skipping leading white-space, read up to 127 char. Except do not read in a '\n'. Store the result, with an appended '\0' to stock[startrec].id.

"%*c" Without skipping leading white-space, read any 1 char. This is either the '\n' that stopped the preceding or we are now in an EOF condition. '*' means to not save the result.

Or better yet...

Use fgets(), trimming the typical trailing \n as needed.

fgets(stock[startrec].id, sizeof stock[startrec].id, fp);

Suggest checking the results of fscanf() and fgets() and dropping feof()

printf("i");
if (fp != NULL) {
  int cnt;
  while((cnt = fscanf(fp, "%s", stock[startrec].id)) != EOF) {
    if (cnt < 1) Handle_NothingWasRead();
    printf("%s", stock[startrec].id);
    printf(" \n");
    getch();
    startrec = startrec + 1;
    allrec = startrec;
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • This question fixed my problem, thank you – user3227362 Jan 27 '14 at 09:39
  • @user3227362 After posting (hours, or days, etc.) and when an answer well meets your needs, [accept](http://stackoverflow.com/help/accepted-answer) it. After you earn 15+ rep points, consider up-voting all answers that you find especially helpful. – chux - Reinstate Monica Jan 27 '14 at 15:23