الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

سؤال في اضافة سطور جديدة الى الملفات

مغلق
بدأه No_Fear في 3 ديسمبر 2005 · 4 رد · 450 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

Can you help me with this!!

This is a C program, where the main program is written at the beginning then you have five functions at the bottom, called by the main program. The program creates a file called result.txt once it is run.

I use cygwin for that, you need to locate the file first as if you are using DOS, then type gcc main1.c and press enter followed by ./a then enter to run the program. You’ll get a message saying "file result.txt created!!" then a file will be created in the same folder you saved the program in.

Open the result.txt file you'll be able to know what I want from you.. As you can see all the results are written in one continues line!! Why is that?? I couldn’t figure out what’s wrong or even how can I fix this problem. The results of each function should be printed in different line.

I’m attaching the program and a modified-result.txt file.. I want the file created to look like this one.. can you help!!

#2

ملف ال main.c

#include <stdio.h>

#include <stdlib.h>

void function3(double even);

void function9(double previous_salary, double pt);

void function13(double avg); /* function declaration - prototype*/

void function16(int number);

void function18(double pay);

FILE*result; /*creating an output file*/

int main()

/*Main Program*/

{

int your_number=4107, exnumber=4107;

result=fopen("result.txt", "w");

if (result==NULL)

{

printf("\nThere Was A Problem When Opening The Result .txt file!!!");

/*If there is an error opening the file, the program terminates*/

exit(1);

}

printf("File Result.txt created!!");

fprintf(result,"\nThis Is The Result File For Student Numberex0%d And

The-number is %d\n", exnumber, your_number);

/* caliing functions */

function3(8);

function9(16428, 0.07);

function13(3);

function16(4107);

function18(19.730);

fclose(result);

return 0;

}

/*Main Program ends and functions start*/

/* */

void function3(double even) /*definition of function 3*/

{

double sum=0, avg;

int a=7, b=41; /*declaring variables*/

int counter;

counter=0;

fprintf(result,"\nResults For Function 3:\n");

do

{

sum=sum+even;

even=even+2;

counter=counter+1;

avg=sum/counter;

}

while(even<=41);

fprintf(result,"\nThe Sum Of The Even Entegers Between %d And %d is %lf",

a, b, sum);

fprintf(result,"\nAnd The Average Is %lf",avg);

}

/* */

/* */

void function9(double previous_salary, double pt) /*definition of function

9*/

{

double p_rise, sumy, summ, sumw; /*declaring variables*/

fprintf(result,"\nResults For Function 9:\n");

previous_salary=16428;

pt=0.07;

p_rise=previous_salary*pt;

sumy= previous_salary + p_rise;

summ= sumy / 12;

sumw= sumy /52;

fprintf(result,"\nThe New Annual Salary is %f, \nThe New Monthly Salary is

%f, \nThe New Weekly Salary is%f", sumy, summ, sumw);

}

/* */

/* */

void function13(double avg) /*definition of function 13*/

{

int arr[4]={4, 1, 0, 7}; /*declaring variables*/

int i, sum=0;

fprintf(result,"\nResult of Function 13:\n");

for(i=0; i<4; i++)

{

sum= sum + arr;

}

avg=sum/4;

fprintf(result,"\n1st Element \t2nd Element \t3rd Element \t4th

Element");

fprintf(result,"\n%d \t\t%d \t\t%d \t\t%d", arr[3], arr[2], arr[1],

arr[0]);

fprintf(result,"\n\nThe Average of The Elements Of The Array is %lf\n",

avg);

}

/* */

/* */

void function16(int number) /*definition of function 16*/

{

int arr[4]={4, 1, 0, 7}; /*declaring variables*/

fprintf(result,"\nResult of Function 16:\n");

fprintf(result,"The Input Number Is %d, \n", number);

fprintf(result,"And This Number In Reverse Order Is %d%d%d%d", arr[3],

arr[2], arr[1], arr[0]);

}

/* */

/* */

void function18(double pay) /*definition of function 18*/

{

int arr[4]={4, 1, 0, 7};

int otd, ote; /*declaring variables*/

double wd_over, we_over, normal_pay, over_pay, tax;

double totpay, salary;

fprintf(result,"\nResult of Function 18:\n");

otd=arr[3]+6; /*over time at weekdays*/

ote=arr[0]+1; /*over time at weekends*/

normal_pay=pay*35;

wd_over=pay*1.25*otd;

we_over=pay*1.5*ote;

totpay = normal_pay + wd_over + we_over;

tax= ((totpay-500)*0.5) + (300*0.4) + (200*0.3);

over_pay = wd_over + we_over;

salary = totpay - tax;

fprintf(result,"Normal pay/W: %lf, Overtime pay/W: %lf, Total tax/W:

%lf\n", normal_pay, over_pay, tax);

fprintf(result,"The Total Week Income After Tax Deduction: %lf\n", salary);

}

/* */

/*End of functions and program*/

#3

ملف resultfinal.txt

This Is The Result File For Student Numberex04107 And The-number is 4107

Results For Function 3:

The Sum Of The Even Entegers Between 7 And 41 is 408.000000

And The Average Is 24.000000

Results For Function 9:

The New Annual Salary is 17577.960000,

The New Monthly Salary is 1464.830000,

The New Weekly Salary is338.037692

Result of Function 13:

1st Element 2nd Element 3rd Element 4th Element

7 0 1 4

The Average of The Elements Of The Array is 3.000000

Result of Function 16:

The Input Number Is 4107,

And This Number In Reverse Order Is 7014

Result of Function 18:

Normal pay/W: 690.550000, Overtime pay/W: 468.587500, Total tax/W:

509.568750

The Total Week Income After Tax Deduction: 649.568750

#4

أخي الكريم, السلام عليكم و مرحبا بك في المنتدى.

أخي موضوعك مخالف, نرجو منك قراءة قواعد المشاركة, سأقوم بتغيير عنوان الموضوع و لكن أرجوك ان تنتبه للقواعد في المرات القادمة.

بالنسبة للسؤال, فنظام الوندوز يستخدم "\r\n" في الملفات من أجل اضافة سطور جديدة, بينما في اللينكس يستخدم "\n" فقط من أجل الفصل بين السطور.

مثلا,

fprintf(result,"\nResult of Function 18:\n");

حولها إلى:

fprintf(result,"\r\nResult of Function 18:\r\n");

يمكن مراجعة:

http://en.wikipedia.org/wiki/Newline

Software applications and operating systems usually represent the newline with one or two control characters:

Systems based on ASCII or a compatible character set use either LF (Line Feed, 0x0A) or CR (Carriage Return, 0x0D) individually, or CR followed by LF (CR+LF, 0x0D 0x0A). 
LF:    Unix and Unix-like systems, Xenix, Mac OS X, BeOS, Amiga, RISC OS and others 
CR+LF: CP/M, MP/M, DOS, Microsoft Windows 
CR:    Apple II family and Mac OS through version 9
#5

تسلم أخوي وماعليك قصوووووووور

هذا الموضوع مغلق.

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…