[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Reformat strings from .txt file

Posted on 1998-02-14
2
Medium Priority
?
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

650 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