Solved

write to file problem

Posted on 2000-02-18
6
340 Views
Last Modified: 2012-05-04
Referring to the code below
I have to run the code like
app.exe 1 23 8
so the value
argv[1] = 1
argv[2] = 23
argv[3] = 8
so what is the following code to and up let say add up
a) first two value  
b) add all value

and write the two calculate value to a file

THANX IN ADVANCE



#include <iostream.h>
#include <stdio.h>
#include <string.h>

int main( int argc, char *argv[], char *envp[] )
{
    char input[9];FILE *stream;
    int iNumberLines = 0;    // Default is no line numbers.
    // If more than .EXE filename supplied, and if the
    // /n command-line option is specified, the listing
    // of environment variables is line-numbered.

    if( argc == 2 && stricmp( argv[1], "/n" ) == 0 )
        iNumberLines = 1;

    // Walk through list of strings until a NULL is encountered.
    for( int i = 0; envp[i] != NULL; ++i )
    {
        if( iNumberLines )
            cout << i << ": " << envp[i] << "\n";
    }
}

0
Comment
Question by:winson
[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
  • 3
  • 3
6 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2535770
We cannot provide answers to school assignments.  That is grounds for removal from this site.  (for both you and the experts involved.)  We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2535773
I'll tell you this much for now.

To write a number to a file you need to use an fstream object like so

#include <fstream>
using namespace std;

int main()
{
   fstream File("thefilename",ios_base::out);
   int X = 5;

   File << X;
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 2535798
A bit more.  The values to be added together are specified as parameters to the program in the argv[] array.  This means the values are all character strings.  i.e they are characters that express the value, not the binary representation of the value.  i.e. the values are expresed like

char *AValue = "1999";

not like

int x = 1999;

The computer can't do math on a string anymore than you can do math like
  "one + "five"

The strings need to be converted to the integer values they represent.  (like you would convert "two" to 2).  This can be done using the atoi() function.

If that doesn't help enough, try to ask more specific questions.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 300 total points
ID: 2535802
Next is code:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main( int argc, char *argv[])
{
    int lpN[3];
    FILE *stream;
    memset(lpN, 0, sizeof(lpN));
    // If more than .EXE filename supplied, and if the
    // /n command-line option is specified, the listing
    // of environment variables is line-numbered.

    // Walk through list of strings until a NULL is encountered.
    for( int i = 1; i < argc; i++ )
     {
        cout << i << ": " << argv[i] << "\n";
        lpN[i-1] = atoi(argv[i]);
     }
    stream = fopen ("mtfile.txt","wt");
    if (stream)
      {
        fprintf(stream, "%d\n", lpN[0] + lpN[1]);
        fprintf(stream, "%d\n", lpN[0] + lpN[1] + lpN[2]);
        fclose(stream);
      }
}
I hope, it helps. Alex
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2535811
O , i see, nietod think , that it is hopework!
Winson, is it true?
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2540178
nu, after 'd', i see, that it is really
homework...
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

627 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