LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-17-2009, 07:39 PM   #1
NewbatC
LQ Newbie
 
Registered: May 2009
Location: New Hampshire
Posts: 4

Rep: Reputation: 0
Writing to external text file with C or C++


Hi everyone,

I’m certainly glad to have found this place and much credit goes out to the developer and care taker of this forum not to mention all those who partake in all the answers! Thank you - all of you!

I do have a question though, or several… Any info would be great. My specialty has been CSS, Javascript, HTML, XHTML and only very little C and C++. However, I am pressed for time on this project and I thought the day would never come when I would need to use C or C++ in a users application, so here it goes;

Basically, in a nut shell, I am trying to write a C and/or C++ (perhaps this may not be the best language to go with, I’m not sure) script that will output multiple Javascript string arrays in one single text file, without erasing or overwriting any previous text strings within that .txt file even when called upon again.

Here is a sample of the type of string(s) that I would like to see that would essentially be the result after being saved to the file;

s[0] = "Prodoct Name^Name.doc^<br>Part Num<br>Order Num<br>mm/dd/yy:";
s[1] = "Prodoct Name^Name.pdf^<br>Part Num<br>Order Num<br> mm/dd/yy:";
s[2] = "Prodoct Name^Name.xls^<br>Part Num<br>Order Num<br> mm/dd/yy:";
s[3] = "Prodoct Name^Name.htm^<br>Part Num<br>Order Num<br>mm/dd/yy:";
etc.,
etc.,
etc,

The way I have thought about writing the program (Which I have had much difficulty between extra characters, extra whitespace, or nothing at all) would ask the user, line by line, as to what data the user wants to enter. The script begins by asking the user;

“Start a New String [1]”
“Read Database File [2]”
“Quit Program [3]”

“Please select (1, 2, 3):_”

If the user selects “3”, then presses [ENTER], the program quits.

When the user enters “2”, then presses [ENTER], the program reads all of the data in the file and displays it like so on the screen;

s[0] = "Prodoct Name^Name.doc^<br>Part Num<br>Order Num<br>mm/dd/yy:";
s[1] = "Prodoct Name^Name.pdf^<br>Part Num<br>Order Num<br> mm/dd/yy:";
s[2] = "Prodoct Name^Name.xls^<br>Part Num<br>Order Num<br> mm/dd/yy:";
s[3] = "Prodoct Name^Name.htm^<br>Part Num<br>Order Num<br>mm/dd/yy:";

Or can be displayed this way to avoid taking up too much real estate on the screen;

s[0]
s[1]
s[2]
s[3]
s[4]

Technically, by reading the file, this will show the user the last string that was written, in the event that user needs to type in the “s[X]=” manually.

When the user enters “1”, then presses [ENTER], the script inserts or begins the new string with “s[0]=”. (If not, I would settle for inserting the string manually when prompted).

However, the external text file would show:

s[0] = “

Program returns these prompts;

“New string array successfully started!

1. “What is the name of the product?:_”

The user enters the name of the product (blabla), presses [enter], then the program continues to write on the same line in the text file. After the “blabla” is written in the text file, the program also writes “^” just after the blabla.

In the text file, the result would be like this:

s[0] = “blabla^

Program returns the statement;

“Product name: blabla successfully entered!”

Then returns this prompt;

2. “Name of document and extension? (Name.doc, Name.pdf, etc.):_ “

The user then types in the name of the document and the extension (blabla.doc), then presses [enter] and the program writes the document and extension name to the external text file on the same line, and also writes “^<br>” after the blabla.doc.

In the text file, the result would show:

s[0] = “blabla^ blabla.doc^<br>

Program returns this statement;

“The Document name blabla.doc was successfully entered!”

Then returns this prompt;

3. “What is the part number?:_”

The user types in the part number, presses [enter], and the program also writes “:<br>” after the 123456 number.

In the text file, the result is shown:

s[0] = “blabla^ blabla.doc^<br>123456<br>


Program returns this statement;

“Part number “123456” successfully entered!”

Then returns this prompt;

4. “What is the Order Number?_”

The user now types in the order number (123456), presses [enter] and the program also writes another “<br>” at the end of the order number.

In the text file, the result shows:

s[0] = “blabla^ blabla.doc^<br>123456<br>123456<br>

Program returns this statement;

“Order number “123456” successfully entered!”

Then returns this prompt;

5. “What is the start date?:”

Finally, the user now types in the start date “MM/DD/YY”, presses [enter] and the program also writes :”; at the end of the date.


The external text file now shows the final result of the string array;

s[0] = “blabla^blabla.doc^<br>123456<br>123456<br>mm/dd/yy:”;

Program returns the final statement;

“String has been successfully finished!”

Returns this prompt;

6. “Would you like to start a new array (Y/N):__”.

If the user selects N, this will quit the program, but if the user selects “Y”, this will start and append a new string on the next or new line as the external text file would show; (Even if the new string array cannot be incremented between the brackets, having a command to request the user to insert the number between the brackets would suffice if this makes it easier).

s[0] = “blabla^blabla.doc^<br>123456<br>123456<br>mm/dd/yy:”;
s[1] = “

In all, I am certain that the way that this script needs to be constructed is with appending text in mind as I don’t want to erase any data when starting a new string variable. I’ve tried using Javascript to perform the above functions, but I saw the ability with C and or C++ and felt that was a better way to go. But admittedly, I am lost. Perhaps C or C++ may not be the way to go? I am uncertain that other functions could be added for “switch and case”. As for now I have scrapped the code because of difficulty and time constraints.

Right now, I have no way for the program to remember the last variable string written and how to make it start on the next line, rather than at the end of the last string written. I have been working on this for a few weeks, and have I’ve gotten nowhere, therefore the code that I currently have would do no justice posting here. I’d rather take lessons from the pros out here…

The most difficult for me, is that I am a newbie to C or C++ and have only used very little for past applications in web design. If anyone can come through with this, I promise to credit your name in this application. Any ideas, helpful code, suggestions or another direction that any of you think should be taken would be greatly appreciated.

Last edited by NewbatC; 05-18-2009 at 10:59 AM.
 
Old 05-17-2009, 10:00 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by NewbatC View Post
I do have a question though, or several
lol! If you were at all familiar with C or C++, the answer to your question would be obvious to you.

If you really need to solve this problem using C or C++, you should buy and read "C Programming Language (2nd Edition)" by the famous Kernighan and Ritchie.

I write a lot of C code for my day job, and I would solve your problem using a simple Perl script.

Best of luck!
 
Old 05-17-2009, 10:10 PM   #3
archShade
Member
 
Registered: Mar 2006
Location: Delft NL
Distribution: Debian; Slackware; windows 7
Posts: 218

Rep: Reputation: 53
As a starting point in C++ you need to use the fstream library.

This website is good for C++ references.

here's there tutorial on fstream


Please don't use word urgent in title as it won't help getting fast response people volunteer time here and help when and where they can.

Last edited by archShade; 05-17-2009 at 10:14 PM.
 
Old 05-18-2009, 10:13 AM   #4
NewbatC
LQ Newbie
 
Registered: May 2009
Location: New Hampshire
Posts: 4

Original Poster
Rep: Reputation: 0
I do have a question though, or several

Quote:
Originally Posted by David1357 View Post
lol! If you were at all familiar with C or C++, the answer to your question would be obvious to you.

If you really need to solve this problem using C or C++, you should buy and read "C Programming Language (2nd Edition)" by the famous Kernighan and Ritchie.

I write a lot of C code for my day job, and I would solve your problem using a simple Perl script.

Best of luck!
Thanks for the reply Dave. I guess what I should do is post the C script of what I currently have, but I was hoping to look for fresh new ideas on this matter. If you think that Perl is the way to go, why open the door then close it?
 
Old 05-18-2009, 10:52 AM   #5
NewbatC
LQ Newbie
 
Registered: May 2009
Location: New Hampshire
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by archShade View Post
As a starting point in C++ you need to use the fstream library.

This website is good for C++ references.

here's there tutorial on fstream


Please don't use word urgent in title as it won't help getting fast response people volunteer time here and help when and where they can.
Arch, thank you for the reply. Right now I currently have the program written in C, using the <stdio.h> and <stdlib.h> not C++. I will be posting the script later so that you can get an idea where I am at. Most recently I found an issue with the compiler Bloodshed Dev C\C++ which is what I have been using all along. I have this installed on my laptop and now at my main PC. Tried running the script on both my laptop and my PC, and found that Dev C was not recognizing certain functions that my PC was. Issues where such that Dev C on my laptop (which is what I have been using all along) would not recognize scanf, int, and fprintf. I reloaded the libraries for this, checked to make sure that I had the correct headers, but still, unrecognised. Re-installed Dev C on my laptop and now the script is working, or at least the portion that I now have. Regarding "Urgent", it has been removed, only wrote this in as it was suggested by post instructions. I apologize if this caused any concern...
 
Old 05-18-2009, 04:10 PM   #6
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by NewbatC View Post
If you think that Perl is the way to go, why open the door then close it?
I neither opened nor closed the door, I merely pointed at it.

Your application appears to do a lot of string processing. It is easier to get that right in a short amount of time in scripting language such as Perl.

However, it appeared from your question that you were asking us to write the solution for you. As I mentioned before, I write software for a living. I will answer questions, and help you debug your code. But if you need me to write code from scratch, you will have to pay cash up front.
 
Old 05-19-2009, 01:33 AM   #7
NewbatC
LQ Newbie
 
Registered: May 2009
Location: New Hampshire
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David1357 View Post
I neither opened nor closed the door, I merely pointed at it.

Your application appears to do a lot of string processing. It is easier to get that right in a short amount of time in scripting language such as Perl.

However, it appeared from your question that you were asking us to write the solution for you. As I mentioned before, I write software for a living. I will answer questions, and help you debug your code. But if you need me to write code from scratch, you will have to pay cash up front.
Dave, thanks for your reply and offer. I actually figured it out though and got everything working. I found out that it was Dev C\C++ that was giving me the problem(s) that I was having. Unfortunately, as a new-b, it was a bit trying since picking up from old school days and now really getting back into C just a few weeks ago. The worst part being that that I found Dev C\C++ to be unstable at the same time and questioning if it was me or the compiler. For some reason, I would compile the code and it would compile without error.

Eventually, it would find errors, but in all the wrong places - even without me changing the code. For example, it would pickup errors on "for loops" then later it wouldn't, would pickup "scanf" as error then wouldn't, would write to file then wouldn't, but would not show any errors either, would throw errors as soon as it saw "int",then it wouldn't, then every once in a while it would tell me that one or more libraries where unavailable.

It really became more obvious that something was really wrong when I decided to use a "switch "case" and "break" for a very simple code that I knew worked and threw all sorts of errors. This is where my frustration was causing me uncertainty whether if it was the coding or not. That's when I decided to write here looking for other solutions or fresh ideas that I hadn't thought of or perhaps missed in the code that I was using.

I wound up downloading LabView which has a compiler and everything now works perfectly - even finding the right errors. Again, I certainly appreciate the offer though, and if I hadn't figured it out, I probably would have hired or asked you...
 
Old 05-19-2009, 02:42 AM   #8
cloud9repo
Member
 
Registered: Oct 2008
Location: Middle TN
Posts: 134

Rep: Reputation: 19
gcc is the linux built-in compiler, and has many switches of it's own.

Parameter handling is entirely different from standard.

cmds:

man gcc

and gcc --help

will yield the jist of it.

From what I understand, you're needing array handling, and with c or c++ you need to understand that it's done mainly by arbitrary confinement.

not as in row, column access, but addressing. So, that you can manipulate it more easily. Within RAM, instead of drive access.

sample:

main :
array_name[1,2,4,j++]

will give with j incrementing the superincreasing numbers. This will give you an 'adaptable' j_function.

So, when gcc is launced, your parameters have to be in line with access, including terminal input.

Among all this code reference, take it easy.

If it were me, I'd throw a few wait cycles, and let the RAM settle. People mash keyys.

sample:

funct.time.wait(cls*gets(" "));

is a classic way to prevent someone from key-jamming...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
I need HELP Writing text to a FILE ravi2ray Linux - Newbie 4 10-29-2008 11:10 PM
Problem with writing in a text file in C manolakis Programming 5 12-06-2006 11:18 PM
writing text to the beginning of a file linuxmandrake Linux - Newbie 3 03-26-2006 11:55 PM
Writing to a specific line in a text file mrobertson Programming 6 12-30-2005 05:02 PM
Writing to a text file vb.net mrobertson Programming 5 08-04-2005 01:37 PM


All times are GMT -5. The time now is 10:52 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration