LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2011, 07:21 PM   #1
darkhorse92
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Rep: Reputation: Disabled
wrap lines


This program was done in c(using visual studio) that wraps lines from a text file (breaking the lines to have a maximum of 40 columns). this program was done as a team(but unable to solve the problem at all). The program does not work according to the requirement.The program does not ask for the input.

/* begin wrap.c */
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>

#define MAXLINE 10000
#define MAXWORD 1000

int main(int argc, char* argv[])
{
FILE *inf, *outf;
char c = '\0', cc, d, line[MAXLINE+2], word[MAXWORD+2];
int width, newline = 0, line_pos = 0, word_pos = 0;
if (argc != 4)
{
printf("USAGE: wrap <read_from> <write_to><chars_per_line>\n");
return 0;
}
if (!strcmp(argv[1], argv[2]))
{
printf("ERROR: input and output files must differ\n");
return 0;
}
if (!(inf = fopen(argv[1], "r")))
{
printf("ERROR: can't read from \"%s\"\n", argv[1]);
return 0;
}
if (!(outf = fopen(argv[2], "w")))
{
printf("ERROR: can't write to \"%s\"\n", argv[2]);
return 0;
}
if ((width = atoi(argv[3])) > MAXLINE || width < 1)
{
printf("ERROR: line width must be from 1 to %d, inclusive\n",MAXLINE);
return 0;
}
while (1)
{
d = cc;
c = cc = getc(inf);
if (c == EOF)
{
if (line_pos > 0)
{
line[line_pos] = '\0';
fprintf(outf, "%s\n", line);
}
goto quit;
}
if (c == '\r')
continue;
if (c == '\n')
{
if (d == '\n')
newline = 1;
else
c = ' ';
}
line[line_pos++] = word[word_pos++] = c;
if (isspace(c))
word_pos = 0;
if (word_pos > MAXWORD)
{
word[word_pos] = '\0';
printf("ERROR: \"%s\" exceeds maximum word length (%d)\n",word, MAXWORD);
goto quit;
}
if (newline || line_pos > width)
{
newline = 0;
line_pos -= word_pos;
while (isspace(line[--line_pos]) && line[line_pos] !='\n');
line[line_pos+1] = '\0';
fprintf(outf, "%s\n", line);
strncpy(line, word, word_pos);
line_pos = word_pos;
}
}
quit:;
fclose(outf);
fclose(inf);
return 0;
}
/* end wrap.c */
 
Old 12-12-2011, 07:52 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Hello,
1) use code tags
2) what exactly is the problem? I compiled the code and it seems to work fine. Can you please describe what the program does and how exactly that differs from what you want it to do?
3) you shouldn't return zero if an error occurs

Quote:
The program does not ask for the input.
I don't understand this. What input?
 
Old 12-12-2011, 08:17 PM   #3
darkhorse92
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
Hello,
1) use code tags
2) what exactly is the problem? I compiled the code and it seems to work fine. Can you please describe what the program does and how exactly that differs from what you want it to do?
3) you shouldn't return zero if an error occurs



I don't understand this. What input?

this program should take a .txt file from the users storage (the .txt contains some words like "hello this is misterxyz and i am here for help " ) and then the duty of the code is to make words to go into next line (this is called word wrap)... so one told me i needed -s and -w(width) to get the word to split and go to next line..

the output given by the program should be somewhat like and this output is made into another .txt file by the program
"hello
this is
misterxyz and
i am here for
help"
 
Old 12-13-2011, 06:53 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by darkhorse92 View Post
this program should take a .txt file from the users storage (the .txt contains some words like "hello this is misterxyz and i am here for help " ) and then the duty of the code is to make words to go into next line (this is called word wrap)...
And that is exactly what the program does:
Code:
$ cat > data.1
"hello this is misterxyz and i am here for help " 
$ ./wrap data.1 data.2 20
$ cat data.2
"hello this is
misterxyz and i am
here for help "
I would say the program is working correctly.
A few details:
1)
Code:
char line[MAXLINE+2]
why +2?

2)
Code:
d = cc;
c = cc = getc(inf);
In the first iteration, the value of cc will be uninitialized. What is the point of the cc variable anyway? It seems redundant.

3) Another potential problem:
Code:
line_pos -= word_pos;
while (isspace(line[--line_pos]) && line[line_pos] !='\n');
what if line_pos == word_pos?
 
Old 12-13-2011, 09:26 AM   #5
darkhorse92
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
And that is exactly what the program does:
Code:
$ cat > data.1
"hello this is misterxyz and i am here for help " 
$ ./wrap data.1 data.2 20
$ cat data.2
"hello this is
misterxyz and i am
here for help "
I would say the program is working correctly.
A few details:
1)
Code:
char line[MAXLINE+2]
why +2?

2)
Code:
d = cc;
c = cc = getc(inf);
In the first iteration, the value of cc will be uninitialized. What is the point of the cc variable anyway? It seems redundant.

3) Another potential problem:
Code:
line_pos -= word_pos;
while (isspace(line[--line_pos]) && line[line_pos] !='\n');
what if line_pos == word_pos?

Which program are you using to compile it?
When i use Microsoft visual studio it gives me as command
"USAGE: wrap <read_from> <write_to><chars_per_line>"
press any key to continue....


for the question you asked
1) this will not cause any problem
2) i am not sure about this "cc"
3) the changing in the code didn't make any difference.
 
Old 12-13-2011, 09:55 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by darkhorse92 View Post
Which program are you using to compile it?
I used gcc.

Quote:
Originally Posted by darkhorse92 View Post
When i use Microsoft visual studio it gives me as command
"USAGE: wrap <read_from> <write_to><chars_per_line>"
press any key to continue....
That means that (argc != 4). Program needs 3 arguments to run:
i) the name of the input file
ii) the name of the output file
iii) the max number of characters per line
You need to launch the program from the command line with these arguments.
Alternatively, you may specify these arguments somewhere in the Visual Studio (I don't know where exactly since I don't use VS)


Quote:
Originally Posted by darkhorse92 View Post
3) the changing in the code didn't make any difference.
If a word is longer than the desired width, then line_pos will be equal to word_pos at that part of the code.
line[--line_pos] will then access memory just before the begining of line. The fact that the data at that address does not happen to correspond to a whitespace character is just luck and doesn't make it a good programming practice.
 
1 members found this post helpful.
Old 12-13-2011, 04:35 PM   #7
darkhorse92
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
I used gcc.



That means that (argc != 4). Program needs 3 arguments to run:
i) the name of the input file
ii) the name of the output file
iii) the max number of characters per line
You need to launch the program from the command line with these arguments.
Alternatively, you may specify these arguments somewhere in the Visual Studio (I don't know where exactly since I don't use VS)



If a word is longer than the desired width, then line_pos will be equal to word_pos at that part of the code.
line[--line_pos] will then access memory just before the begining of line. The fact that the data at that address does not happen to correspond to a whitespace character is just luck and doesn't make it a good programming practice.

is there anyway you can fix this problem so that it can compile in VS
(i have noticed one more problem that goto is a global variable and its not good to add global variables into a code.
 
Old 12-13-2011, 10:50 PM   #8
darkhorse92
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
I used gcc.



That means that (argc != 4). Program needs 3 arguments to run:
i) the name of the input file
ii) the name of the output file
iii) the max number of characters per line
You need to launch the program from the command line with these arguments.
Alternatively, you may specify these arguments somewhere in the Visual Studio (I don't know where exactly since I don't use VS)



If a word is longer than the desired width, then line_pos will be equal to word_pos at that part of the code.
line[--line_pos] will then access memory just before the begining of line. The fact that the data at that address does not happen to correspond to a whitespace character is just luck and doesn't make it a good programming practice.

how to launch the program from the command line?.
suppose that my input file is testwork.txt
and my output file is newtestwork.txt
and let the character per line be 40.....

i have specify the argument in the VS in the project properties "testwork.txt" but i got the same result when i complied it...
 
Old 12-14-2011, 04:48 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by darkhorse92 View Post
is there anyway you can fix this problem so that it can compile in VS
The code compiles correctly. What you're getting is a runtime error.

Quote:
Originally Posted by darkhorse92 View Post
i have noticed one more problem that goto is a global variable and its not good to add global variables into a code.
That's right. You should not make your variables global if you can avoid it.
However, goto is not a variable, it's a reserved keyword for jump. goto quit makes the program "jump" to a place marked by a label quit. Excessive usage of jumps is also generally not encouraged, because it can lead to spaghetti code.


Quote:
Originally Posted by darkhorse92 View Post
how to launch the program from the command line?.
1) Open the command line
2) cd to the directory containing your executable
3) execute the program with the arguments as the error message suggests

So, assuming that the executable is called WRAP.EXE and it's in the path C:\PATH\TO\THE\PROGRAM:
Code:
CD C:\PATH\TO\THE\PROGRAM
WRAP.EXE testwork.txt newtestwork.txt 40
Quote:
Originally Posted by darkhorse92 View Post
i have specify the argument in the VS in the project properties "testwork.txt" but i got the same result when i complied it...
There must be 3 arguments. It should be something like Project->Properties->Debug and in the box Command line arguments you just enter all three arguments separated by spaces: testwork.txt newtestwork.txt 40.
 
1 members found this post helpful.
Old 12-14-2011, 09:04 AM   #10
darkhorse92
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
The code compiles correctly. What you're getting is a runtime error.



That's right. You should not make your variables global if you can avoid it.
However, goto is not a variable, it's a reserved keyword for jump. goto quit makes the program "jump" to a place marked by a label quit. Excessive usage of jumps is also generally not encouraged, because it can lead to spaghetti code.




1) Open the command line
2) cd to the directory containing your executable
3) execute the program with the arguments as the error message suggests

So, assuming that the executable is called WRAP.EXE and it's in the path C:\PATH\TO\THE\PROGRAM:
Code:
CD C:\PATH\TO\THE\PROGRAM
WRAP.EXE testwork.txt newtestwork.txt 40


There must be 3 arguments. It should be something like Project->Properties->Debug and in the box Command line arguments you just enter all three arguments separated by spaces: testwork.txt newtestwork.txt 40.
So here is what i got the error
Run-Time Check Failure #3-the variable "cc" is used without being initialized
 
Old 12-14-2011, 09:19 AM   #11
darkhorse92
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
The code compiles correctly. What you're getting is a runtime error.



That's right. You should not make your variables global if you can avoid it.
However, goto is not a variable, it's a reserved keyword for jump. goto quit makes the program "jump" to a place marked by a label quit. Excessive usage of jumps is also generally not encouraged, because it can lead to spaghetti code.




1) Open the command line
2) cd to the directory containing your executable
3) execute the program with the arguments as the error message suggests

So, assuming that the executable is called WRAP.EXE and it's in the path C:\PATH\TO\THE\PROGRAM:
Code:
CD C:\PATH\TO\THE\PROGRAM
WRAP.EXE testwork.txt newtestwork.txt 40


There must be 3 arguments. It should be something like Project->Properties->Debug and in the box Command line arguments you just enter all three arguments separated by spaces: testwork.txt newtestwork.txt 40.
After the placing the arguments in the Command Line arguments, the working started to work even it made the newtestwork.txt but the program crashed saying "Run-Time Check Failure #3-the variable "cc" is used without being initialized"
 
Old 12-14-2011, 09:22 AM   #12
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Just initialize the cc variable when you declare it, something like

Code:
char c = '\0', cc = 0, d, line[MAXLINE+2], word[MAXWORD+2];
or, even better, remove it, because it serves no purpose whatsoever.
 
2 members found this post helpful.
Old 12-14-2011, 09:35 AM   #13
darkhorse92
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
Just initialize the cc variable when you declare it, something like

Code:
char c = '\0', cc = 0, d, line[MAXLINE+2], word[MAXWORD+2];
or, even better, remove it, because it serves no purpose whatsoever.
Is there any way we can get rid of the "goto" without harming the code?
 
Old 12-14-2011, 10:19 AM   #14
darkhorse92
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by darkhorse92 View Post
Is there any way we can get rid of the "goto" without harming the code?
What i mean is that the program works now correctly, however i was thinking to remove the "goto" from the whole code, how will the code change if goto is removed completely?
 
Old 12-14-2011, 10:37 AM   #15
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
you can either replace the goto quit by a break statement, or you can paste the

Code:
fclose(outf);
fclose(inf);
return 0;
where in the place of the goto statements. Once you replace the gotos, you may also remove the quit: label.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Console does not wrap long lines z-vet Linux - Software 11 10-10-2013 04:05 AM
Another awk question: un-word-wrap lines David the H. Linux - General 16 12-16-2007 01:12 PM
wrap lines at 80 for long aliases in .aliases.csh jhwilliams Linux - Software 0 07-26-2007 07:49 AM
"enscript --word-wrap" does not wrap line of text file powah Linux - General 3 05-16-2006 09:12 PM
bash does not wrap long lines correctly monkeyman2000 Linux - General 8 09-08-2004 09:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration