473,394 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

I need to put file into 2D array and then transpose it.

Input File
Expand|Select|Wrap|Line Numbers
  1. 3 7 2 9 0
  2. 1 8 4 6 5
  3. 0 2 7 4 3
  4. 6 1 9 5 8
then, input manually 5 digits: 24161
So, the array will have to be 5x5 in the end. Im a beginner at C++ and having a real tough time.
Program so far.
Expand|Select|Wrap|Line Numbers
  1. #include<iostream> 
  2. #include<fstream>
  3. #include<cstdlib>
  4. #include<string>
  5. #include<sstream>
  6. #include<vector>
  7. using namespace std;
  8. ifstream inFile;
  9. int  i, ID = 0, matrix[5][3], j, k;
  10.  
  11.  
  12.  
  13.  
  14.  
  15. int main()
  16. {
  17.     string b = "";
  18.     string ifilename, line, ofilename;
  19.     ifstream inFile, checkOutFile;
  20.     ofstream outFile;
  21.     char response;
  22.     // Input file
  23.     cout << "Please enter the name of the file you wish to open : ";
  24.     cin >> ifilename;
  25.     inFile.open(ifilename.c_str());
  26.     if (inFile.fail())
  27.     {
  28.         cout << "The file " << ifilename << " was not successfully opened." << endl;
  29.         cout << "Please check the path and name of the file. " << endl;
  30.         exit(1);
  31.     }
  32.     else
  33.     {
  34.         cout << "The file is successfully opened." << endl;
  35.     }
  36.     // Output file
  37.     cout << "Please enter the name of the file you wish to write : ";
  38.     cin >> ofilename;
  39.     checkOutFile.open(ofilename.c_str());
  40.     if (!checkOutFile.fail())
  41.     {
  42.         cout << "A file " << ofilename << " exists.\nDo you want to continue and overwrite it? (y/n) : ";
  43.         cin >> response;
  44.         if (tolower(response) == 'n')
  45.         {
  46.             cout << "The existing file will not be overwritten. " << endl;
  47.             exit(1);
  48.         }
  49.     }
  50.     outFile.open(ofilename.c_str());
  51.     if (outFile.fail())
  52.     {
  53.         cout << "The file " << ofilename << " was not successfully opened." << endl;
  54.         cout << "Please check the path and name of the file. " << endl;
  55.         exit(1);
  56.     }
  57.     else
  58.     {
  59.         cout << "The file is successfully opened." << endl;
  60.     }
  61.     // Copy file contents from inFile to outFile
  62.  
  63.     cout << "Please enter the last 5 digits in your student ID : ";
  64.     cin >> ID;
  65.     cout << " " << endl;
  66.     cout << "The numbers from the input file with added last row." << endl;
  67.  
  68.     while (getline(inFile, line))
  69.         cout << line << endl;
  70.  
  71.  
  72.     stringstream ss;
  73.     if (ID < 0)
  74.         ID = -ID;
  75.     ss << ID; //convert to individual numbers
  76.     ss >> b;
  77.     for (int i = 0; i < b.length(); ++i)
  78.     {
  79.         cout << b[i] << " ";
  80.     }
  81.     cout << " " << endl;
  82.     cout << " " << endl;
  83.  
  84.     cout << "The transposed numbers." << endl;
  85.     cout << " " << endl;
  86.  
  87.  
  88.  
  89.     inFile.close();
  90.     outFile.close();
  91. } // main
Apr 22 '14 #1
0 1139

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: googlinggoogler | last post by:
Hiya, The title says it all really, but im a newbie to python sort of. I can read in files and write files no probs. But what I want to do is read in a couple of files and output them to one...
1
by: Jim H | last post by:
I am on a project where I am supposed to send an XML document to a SQL Server stored procedure. The XML Doc is a list strings. If in my c# function I get a list values as (string psValueList),...
13
by: gavino | last post by:
This seems easy but I have been asking tcl and python IRC chat all day and no one gave an answer. I have 100 servers which need a new backup server added to a text file, and then the backup agent...
4
by: abhanjee | last post by:
Hello, I am a newbie and am developing an application for work. I have figured out how to upload a file attachment to the SQL database and download the same file using C# and Visual Studio....
2
by: eric dexter | last post by:
I need to take text from one file and then save it to a different file. I also need to return any comment The def should have variables for the file that should be read in, the file it should...
13
by: rsk | last post by:
Hi Friends, My requirement is as follows; A file is consisting of data in hexadecimal format(i.e a 32 bit data for example like "0xdeadbeef"). I have to read each of such data into my 'c'...
7
mrnn
by: mrnn | last post by:
Hello. I'm workin on a 2d game where the levels are Mario style and will either take up only the screen or be a sidescroller like Mario is (as in the level goes off the main screen) and the levels...
6
n8kindt
by: n8kindt | last post by:
i have an email program set up that is discussed in Bytes:array for email recipients thread. here's a picture to get the visual of what i'm trying to do ...
2
by: =?Utf-8?B?SmFzb24gQmFybmV0dA==?= | last post by:
I wrote an aspx page that creates a .pdf file upon request and transfers the file to the client for viewing (via an Adobe Acrobat plug-in). Afterwards, the file is no longer needed and should be...
3
by: LordHog | last post by:
Hello, How would I go about finding the default handler, let's say a text file (*.txt), then launch the default handler with the file as an argument? I had found how to launch an external...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.