Solved

Reformat strings from .txt file

Posted on 1998-02-14
2
195 Views
Last Modified: 2010-04-02
I need a way to reformat text from a text file: ie. if there are 2 paragraphs in the file that have 50 lines each, and each line contained 50 or more characters each; how do I reformat the text output to no more than 40 characters to each line, without chopping of entire words?
0
Comment
Question by:trying
2 Comments
 
LVL 1

Expert Comment

by:Nexial
ID: 1257262
Without actually writing the code I would use the following schema:

Use filelength to get the length of the file (as this is a small
file)
Allocate a buffer somewhat greater in size than this length.
Use the fgets function to read each successive line into the
buffer, where the start point of each buffer is the first location of the end_of_line character(s) on your system.   If you
have EOL = '\n' (which is usual), then each successive buffer will start at the '\n' location of the previous read.   This process will eliminate all EOL characters in the file except for the last one, which will be followed by a terminating '\0'

Once EOF has been reached on the input file, and the input file closed then:

Open the output file
Set initial_char_pointer = start_of_buffer
loop:
   Set end_char_pointer = initial_char_pointer+40
   Scan backwards using end_char_pointer for whitespace
   upon first whitespace found, replace whitespace with '\0'
   issue fputs from initial_char pointer.
   set initial_char_pointer to new end_char_pointer+1
   until all done.

Close the file.
           
0
 
LVL 2

Accepted Solution

by:
Ready4Dis earned 50 total points
ID: 1257263
#include <stdio.h>
#include <conio.h>

main ()
{
   FILE *in, *out;
   char word[40];
   int x, ctr, line;
   fopen("infile.txt","rb");
   fopen("outfile.txt","wb");
   while (!feof(in))
   {
     x++;
     line++;
     word[x]=getc(in);
     if (line>=40)
     {
       line=0;
       putc(13,out);
     }
     if (word[x]==32||word[x]=13)
     {
       memset(word,0,40);
       for (ctr=1;ctr<=x;ctr++)
            putc(word[x],out);
       x=0;
     }
   }
   return 0;
}
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Finding a good hash function 4 127
Passing command line arguments in C 10 96
C Language combined operators 28 115
How to access ANSI/IEEE Std 754 or equivalent information ? 3 73
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question