Link to home
Start Free TrialLog in
Avatar of mltm
mltm

asked on

help me (program convert file to a plain c program)

I want a program that reads the programs given in the slides with line numbers plus column from a file and converts the files to a plain C program by removing line numbers.Example:

1:#include <stdio.h>
2:main()
3:{
4:printf("1");
5:}

  it will be written to a file and then will be read and in the new file there won't be 1:  , 2: , 3:
Avatar of sunnycoder
sunnycoder
Flag of India image

char buffer[512];
FILE * infile;
FILE * outfile

infile = fopen ("input file", "r");
outfile = fopen ("output file", "w");

while ( fgets(buffer, sizeof(buffer), infile) != NULL)
     fputs (buffer+2, outfile);
assuming that each line is prefixed by only two characters
incase you have more than 9 lines

char buffer[512];
FILE * infile;
FILE * outfile

infile = fopen ("input file", "r");
outfile = fopen ("output file", "w");

while ( fgets(buffer, sizeof(buffer), infile) != NULL)
    fputs (strchr(buffer,':'), outfile);
Avatar of mltm
mltm

ASKER

can you write the whole code?
ASKER CERTIFIED SOLUTION
Avatar of lorcan
lorcan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Sunnycoder,

Life's not fair huh?  You post a very valid answer and some newbie scarfs up 500 points by posting code that is entirely too long and complex.

500 points.  For THAT????



mltm,

Using Sunnycoder's basic solution you can write a very SHORT program that will allow you to stream (pipe) data into and out of the program so that it is very compatible with other unix and DOS commands:

#include <stdlib.h>
#include <stdio.h>

char Buffer[200];
int main ()
{
  while ( fgets(Buffer, sizeof(buffer), stdin) != NULL)
   fputs (strchr(buffer,':'), stdout);
}


After you compile it to a file called Converter, you convert "oldpgm.c" to "newpgm.c" like this:

Converter < oldpgm.c > newpgm.c


Kent



Sorry, submitted without proofreading.  You need to add 1 to the address returned by strchr().  Try:

#include <stdlib.h>
#include <stdio.h>

char Buffer[200];
int main ()
{
 while ( fgets(Buffer, sizeof(Buffer), stdin) != NULL)
  if (strchr (Buffer, ':'))
    fputs (strchr(Buffer,':')+1, stdout);  // Write the line start with the first character after ':'
  else
    fputs (Buffer, stdout);         // The line didn't contain a ':', write the entire line.
}
right said Kdo ..

Check the accepted answer today morning and was wondering while looking at the long long code "was there more to the question than I had read last might" :-))

but life's like that

mltm perhaps you need to reconsider the way you want to do it

the program posted in the last post by Kdo does all that you had asked in lot less time

>>can you write the whole code?
LOL All you had to do was add #include directives
Avatar of mltm

ASKER

I accepted it because what i need is an exact and a whole answer.I don't look for how i can make a solution in the easiest way,programming is not a thing i want to learn in the best way because what i need is to pass my C lesson.We have only seen the basics of C in a few lessons and the homeworks that are given to us are not the things we can do by only ourselves.And lastly i think it was my question and i have the right to choose my answer.

Since he fooled us into doing a homework assignment for him that he obviously didn't want to do for himself, he deserves the complicated answer.

I'd almost like to be part of his class when he turns it in and can't explain how he got "void maid (void)" past the parser, much less have any clue to how the program works.

;)
>>I'd almost like to be part of his class when he turns it in and can't explain how he
>>got "void maid (void)" past the parser, much less have any clue to how the
>>program works

LOL ... good observation Kent
Hm... what?
I used this stuff in my classes... so he won't have trouble with...
I haven't even thougth of your way of doing, and I must admit, that I've learned a lot!
Thanks and sorry!
It's not my intention to 'steal' your points, sunnycoder!
But it ain't my intention to do your homework, too,mltm!
lorcan you need not be sorry
its not your fault ... It is the judgement of the asker which is under question
and the last thing I could have thought is that you stole my points ... It is mltm's decision (read inappropriate decision)
Avatar of mltm

ASKER

First of all don't call me 'he' because i am a 'she'. I think the rule of here is just a symbolic rule because you can't reallt prove whether it is a homework question or not.Maybe i am interested in the question and want to see the answer.You can never prove that.And if you only talk about the solution not giving it this site is no good to newbies.It becomes just a place to take points showing who is more expert.My instructer wont be asking how i have done it because it is our fourth and last homework and he has not asked till now.He ,himself, told us that  we can get the solutions from internet or books,because the questions are not the kinds that we can do by ourselves with the things we get from the lessons.All the four homeworks i have not done one question myself ,i always found it ready on the internet .We can't say that i don't learn,when i see the code ,i understand the question and learn something. Most of the questions asked here,i bet homework questions but most of the time i see that people give the whole codes.Because unless the asker tells that it is a homework question,noone can prove that it is homework.
Avatar of mltm

ASKER

   Every one is right by his side.I won't disturb you any more,and i won't need your help any more,i thank god,my lesson is over next week and i dont want to see any c in my life.I will post the homework with one lack of question.So adieu!!!!

[tongue in cheek]

mltm has made it very clear that SHE has no interest in C coding.  The fact that she's taking a class that she despises suggests that she's in college, not high school.

She's also made it quite clear that her particular talents aren't English, Math, Science, or Law Enforcement.

Wonderful.  We've just help another "Art History" major graduate.  Can anyone point HER toward the unemployment line?

[/tongue in cheek]

sigh....
Avatar of mltm

ASKER

 I think you are again wrong because i am a technical university student and i study chemistry.C programming lesson is for every branches in our school.If i would be an engineer, I would not talk like this.Any way the c lesson that is given to us is not enough for any one and if you want to learn,you have to study it out of the school  + my english is quite good when compared to others + you have not helped me.
>C programming lesson is for every branches in our school.
its branch, not branches. and if its for every branch, its not c lesson is, but c lessons are.

>If i would be an engineer, I would not talk like this.
if i were an engineer.

>Any way the c lesson that is given to us is not enough for any one
not easy enough, would be putting it better.

ur english sucks.
and this is an expert speaking ;)

kudos to kdo and sunnycoder... they gave the most efficient code.
Hi snkutty,

Welcome to the forums.

Kent
Avatar of mltm

ASKER

Maybe i have grammer faults but you have not understood the meanings i wanted to give.i cant use if i were an engineer bec. i am still a student ,i talk about my future job.and in the second one ,i dont mean the easiness or hardness,i want to mean the quantity they give to us.and any way,it is not your business to comment about my english.my english is enough and i dont need any better.i dont want to speak it perfectly with no grammer fault.why should i?am i an english? you suck. and i did not like the codes of kdo and sunny,ok? i love longggggggg,longgg codes. no one can make me choose what they want.  (sevgiler,saygýlar çok bilmiþ arkadaþ)
Mltm,

Do you just like abuse?  Is that it?  Why not just leave well enough alone and slink off into the darkness -- you've made no friends here.


As a student, you should realize that your absolute first priority is to learn.  It's not to teach.  It's not to argue.  It's not to antagonize people that are trying to help you.  It is to learn.  And it is somethat at which you do not appear to excel.

No one expects you to be an accomplished C programmer.  Even those of us with years of experience get stuck now and then and ask others for help.  When that happens there's no public ridicule.  Instead there are usually good, quality answers.  Often several of them.

Had you been interested in learning you'd have asked why the codes that sunnycode and I posted were superior to the long answer that you accepted.  Your answer was confrontational and rambling.

We live by rules.  Both in society and within the Experts Exchange forum.  One of the rules of the Exchange is that the experts do not write homework assignments for others.  Every one of the Experts here went through the learning process that you are supposed to be going through now.  The result is that we actually know most of the things that we were supposed to learn and we pass that knowledge on to those that are struggling with the same things that we once struggled with.

You broke those rules.  And you did it deliberately.  One day after initiating this thread, you started another at

https://www.experts-exchange.com/questions/20691098/Need-a-last-help-climate-program.html

where you very clearly posted another homework assignment.  What part of "The Rules of the Board Expressly Prohit Experts from Coding Homework Assignments" is a problem?  Is it language or integrity that is the problem?

The moderator offered you an exception when he stated that your instructor can contact him and state that it is acceptable for others to do your homework.  So far there's been no indication that this has occurred.

You may soon have your degree.  And with it you'll be thrust out into a real world that requires more skills than just recreating known compounds.  Communication skills are perhaps the most critical.  You'll need to be able to communicate with your boss and your peers.  If you expect to ever be in a position to help, supervise, or manage others you'll need the special skills of being able to communicate with those that you're trying to lead.  Integrity is equally important.  Turning in someone else's work might get you through a class or two, but that certainly won't get you a job or keep one.  As soon as your boss realizes that your work is just a repeat of someone else's you'll find yourself unemployed.  And the stigma that comes with that kind of firing can also make you unemployable.  A funny thing about bosses -- they sometimes really do know more than you do.

I'll not wish you luck.  No amount of luck can save you from the hole that you're digging for yourself.  Hard work can, but you seem intent on avoiding that route.

Kent
Avatar of mltm

ASKER

Because i love to say the last word and put the point. pointttt