ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 */
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
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"
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');
$ 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.
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
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.
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.
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...
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
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. gotoquit 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
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
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.
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. gotoquit 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
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. gotoquit 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"
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.