Sponsored Content
Homework and Emergencies Homework & Coursework Questions Program run perfectly without error but cant show anything Post 302940789 by guankin on Thursday 9th of April 2015 03:57:56 AM
Old 04-09-2015
Linux Program run perfectly without error but cant show anything

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

No problem,but it cannot show what I want

2. Relevant commands, code, scripts, algorithms:
wc test.cpp
wc test.cpp | ./test -l

3. The attempts at a solution (include all code and scripts):

Code:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main (int argc, char *argv[])
{
    string str;
    char *str1 = new char[80];
    int SUM=0,count=0;
    string fsize;
    int min=0,max=0;

    getline (cin, str);
    strcpy (str1, str.c_str());
    fsize = strtok (str1," ");
    if(argc==1){
            cout<<"Not enough arguements!";

    }

        for(int i=0;i<argc; i++){

                if(strcmp(str1,"-b")==1){

                    for (int m=0; m<=2; m++)
                        fsize = strtok (NULL, " ");
                    if (atoi(fsize.c_str()) > max) {
                        max = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    if (atoi(fsize.c_str()) < max) {
                        min = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    count++;}
                else if (strcmp(argv[i],"-l")==1){

                    for (int m=0; m<=2; m++)
                        fsize = strtok (NULL, " ");
                    if (atoi(fsize.c_str()) > max) {
                        max = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    if (atoi(fsize.c_str()) < max) {
                        min = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    count++;}
                else if (strcmp(argv[i],"-w")==1){

                    for (int m=0; m<=2; m++)
                        fsize = strtok (NULL, " ");
                    if (atoi(fsize.c_str()) > max) {
                        max = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    if (atoi(fsize.c_str()) < max) {
                        min = atoi(fsize.c_str());
                        SUM=SUM+max;}
                    count++;}
                else
                    cout<<"\nInvalid Input!\n";

                cout << "There are " << count << " files" << endl;
                cout << "Largest file has " << max << " bytes" << endl;
                cout << "Smallest file has " << min << " bytes" << endl;
                cout << "The ToTal is " << SUM  << endl;
	}
	return 0;

}


This is my program,what I want is do this program to check how many bytes,lines and words inside any of the file.
When I type wc test.cpp it will pop out
6 111 12608 test
But when I type wc test.cpp | ./test -l
it give me segmentation fault(core dumped)
Anyone can help me with this?

INTI/Interacting System
 

10 More Discussions You Might Find Interesting

1. Programming

how to run debugging on c program

Can someone help me debug a c program I am running? It gives me segmentation fault. I want to turn on debugging. Can some one give the command to turn it on? Below is the error I get: Segmentation Fault (core dumped) (3 Replies)
Discussion started by: ibeg
3 Replies

2. UNIX for Dummies Questions & Answers

Getting a program to run

Please help, I have tried to run this program countless times and still nothing. Please tell me what I'm doing wrong. $ cat>test count=1 while do echo "5" read number echo $5 let count=count+ 1 done exit 0 ^C$ ksh test $ ^C $ ksh test $ $ nothing happend (6 Replies)
Discussion started by: ctiamaria
6 Replies

3. UNIX for Dummies Questions & Answers

How do i run a program while in Unix?....

Im new and wanted to know if im in the unix terminal and lets say i want to run microsoft word for example. i go in and go into the HD and then keep going and i type ls and see that microsoft word.app is there. how do i run it from that? (4 Replies)
Discussion started by: Corrail
4 Replies

4. UNIX for Dummies Questions & Answers

cannot run program

Hi, I have a program in /opt/local/bin, my path in my .profile is export PATH=/opt/local/bin:/opt/local/sbin:$PATH however when i type the program name it cannot find it, however i know the program is in /opt/local/bin? Thanks (2 Replies)
Discussion started by: c19h28O2
2 Replies

5. Shell Programming and Scripting

CRON is not working perfectly

I scheduled a cron job to run @ 1:00 as follows , 00 01 * * * /app/cbf/CLIF/version-1.0.0.0/scripts/archival.sh Please find the archival.sh script. But cron is throwing a mail stating sqlplus command not found So can anybody suggest what might be the problem ??? (13 Replies)
Discussion started by: manas_ranjan
13 Replies

6. OS X (Apple)

Log file to show if loginhook has run or not

Which log file lists the activity of startup scripts, particularly loginhooks? I am trying hard to find it and I cannot. How can I even tell if my script is running without errors if there isnt a log? Very frusturating! (1 Reply)
Discussion started by: glev2005
1 Replies

7. UNIX for Dummies Questions & Answers

Log in, run program

Hey, im editing the passwd file so that when the user ben logs in it runs my assign program. I changed the last column from to Then when i log in i get... There is no problem with the program because it runs fine when i open it normally. Any help much appreciated. (8 Replies)
Discussion started by: RAFC_99
8 Replies

8. Shell Programming and Scripting

Run a program as another user

I have a bash script that launches another problem. I need to run that program under a certain user account. The script itself is run using 'sudo <scriptname>'. #!/bin/bash myprogram=... exec "$myprogram" How would this be done? (1 Reply)
Discussion started by: Carson Dyle
1 Replies

9. Shell Programming and Scripting

Can't get my program to run -- GC calculator?

have been trying for a few weeks not to get this program running. I am newer to programming and it has definitely been a challenge. I think my problem arises with my if statement. I can get it to append the name to the new file, but it simply appends the whole sequence to the file rather than... (3 Replies)
Discussion started by: haley6719
3 Replies

10. AIX

Error when run makefile to compile C program

I have a make file for C program, which always gives the error ld: 0711-738 ERROR: Input file ../src/file_name.o XCOFF32 object files are not allowed in 64 mode Does anybody know the problem? Thanks for contribution (2 Replies)
Discussion started by: digioleg54
2 Replies
STRTOK(3)						     Linux Programmer's Manual							 STRTOK(3)

NAME
strtok, strtok_r - extract tokens from strings SYNOPSIS
#include <string.h> char *strtok(char *str, const char *delim); char *strtok_r(char *str, const char *delim, char **saveptr); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): strtok_r(): _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE DESCRIPTION
The strtok() function parses a string into a sequence of tokens. On the first call to strtok() the string to be parsed should be specified in str. In each subsequent call that should parse the same string, str should be NULL. The delim argument specifies a set of bytes that delimit the tokens in the parsed string. The caller may specify different strings in delim in successive calls that parse the same string. Each call to strtok() returns a pointer to a null-terminated string containing the next token. This string does not include the delimiting byte. If no more tokens are found, strtok() returns NULL. A sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter. Delimiter bytes at the start or end of the string are ignored. Put another way: the tokens returned by strtok() are always nonempty strings. The strtok_r() function is a reentrant version strtok(). The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls that parse the same string. On the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored. In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call. Different strings may be parsed concurrently using sequences of calls to strtok_r() that specify different saveptr arguments. RETURN VALUE
The strtok() and strtok_r() functions return a pointer to the next token, or NULL if there are no more tokens. CONFORMING TO
strtok() SVr4, POSIX.1-2001, 4.3BSD, C89, C99. strtok_r() POSIX.1-2001. BUGS
Be cautious when using these functions. If you do use them, note that: * These functions modify their first argument. * These functions cannot be used on constant strings. * The identity of the delimiting byte is lost. * The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you. EXAMPLE
The program below uses nested loops that employ strtok_r() to break a string into a two-level hierarchy of tokens. The first command-line argument specifies the string to be parsed. The second argument specifies the delimiter byte(s) to be used to separate that string into "major" tokens. The third argument specifies the delimiter byte(s) to be used to separate the "major" tokens into subtokens. An example of the output produced by this program is the following: $ ./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/' 1: a/bbb///cc --> a --> bbb --> cc 2: xxx --> xxx 3: yyy --> yyy Program source #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { char *str1, *str2, *token, *subtoken; char *saveptr1, *saveptr2; int j; if (argc != 4) { fprintf(stderr, "Usage: %s string delim subdelim ", argv[0]); exit(EXIT_FAILURE); } for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) { token = strtok_r(str1, argv[2], &saveptr1); if (token == NULL) break; printf("%d: %s ", j, token); for (str2 = token; ; str2 = NULL) { subtoken = strtok_r(str2, argv[3], &saveptr2); if (subtoken == NULL) break; printf(" --> %s ", subtoken); } } exit(EXIT_SUCCESS); } Another example program using strtok() can be found in getaddrinfo_a(3). SEE ALSO
index(3), memchr(3), rindex(3), strchr(3), string(3), strpbrk(3), strsep(3), strspn(3), strstr(3), wcstok(3) COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2012-05-10 STRTOK(3)
All times are GMT -4. The time now is 03:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy