1. ## programme, prime factorisation

Hey! I'm a complete beginner and I want to write a programme which does the prime factorisation of numbers. The numbers are in a file. The prime factorization should be in a new file. I have written everything and the programme successfully debugged but the window opened and closed. I don't know what's wrong with it.

This is a part of my code
Code:
```{
int number_ofnumbers=0;
char c;
fscanf(f,"%c", &c);
if (c=='\0')
number_ofnumbers++;

int a[number_ofnumbers];
int i;
for (i=0; i<number_ofnumbers; i++)
{
int j;
int k=0;

int b[100];

if (a[i]>1)
{
for(j=2; j<=sqrt(a[i]); j++)
while ((a[i]% j) == 0)
{
if (sqrt(a[i]) != j)
{
k++;
b[k] = a[i];
}
else
{
k++;
b[k] = j;

}
}```
Could anyone look whether it's ok or not? I'd be very thankful.

2. You mean that the output window closes too soon? You could write a getchar(); before the end of main, so that the window waits for a key to be pressed before closes.

But you should look at your file and not your window I guess

Also, welcome to the forum!

3. well hard to duplicate what is going on, if you dont post the entire code so we can run it, kinda common sense there.....

4. Here's the whole code:
Code:
```#include <math.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
FILE *f;
f = fopen("we.txt","r");

if (f) {
printf ("\The file connot be opened.\n");
return -1;
}
while (!feof(f)) {
int number_ofnumbers=0;
char c;
fscanf(f,"%c", &c);
if (c=='\0')
number_ofnumbers++;

int a[number_ofnumbers];
int i;
for (i=0; i<number_ofnumbers; i++)
{
int j;
int k=0;

int b[100];

if (a[i]>1)
{
for(j=2; j<=sqrt(a[i]); j++)
while ((a[i]% j) == 0)
{
if (sqrt(a[i]) != j)
{
k++;
b[k] = a[i];
}
else
{
k++;
b[k] = j;

}
}

FILE *f2;
int z;
f2 = fopen("output.txt", "r");
for(z = 0; z < k; z++){
fputs("b[z]",f2);
}
}
}
}

fclose (f);
return 0;
}```

5. Using feof to control a loop is not a good a idea. Reason.

Also, you have two pointers for the files but you close only one. Close both.

Moreover, you didn't explain what to you mean, even though you followed the good advice of Crossfire, you should help us understand

6. I open a file with numbers. Then I count how many numbers there are in the file. Then goes the prime factorisation. The programme should show the prime factorisation in the 'output' file.

I changed it but now I get 3 errors and 1 warning.
Code:
```
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
FILE *f;
f = fopen("input.txt","r");

if (!f) {
printf ("\The file connot be opened.\n");
return -1;
}
while (feof(f)==0) {
int number_ofnumbers=0;
char c;
fscanf(f,"%c", &c);
if (c=='\0')
number_ofnumbers++;

int a[number_ofnumbers];
int i;
for (i=0; i<number_ofnumbers; i++)
{
int j;
int k=0;

int b[100];

if (a[i]>1)
{
for(j=2; j<=sqrt(a[i]); j++)
while ((a[i]% j) == 0)
{
if (sqrt(a[i]) != j)
{
k++;
b[k] = a[i];
}
else
{
k++;
b[k] = j;

}
}
fclose (f);

FILE *f2;
int z;
f2 = fopen("output.txt", "r");
for(z = 0; z < k; z++){
fputs("b[z]",f2);
}
}
}
}

fclose (f2);
return 0;
}```

7. You pass as parameter of fputs the string "b[z]" and not the actual element of b[z]!

Maybe it would be easier to use fprintf.

It will be a good idea to post your errors and warnings from now one. Also don't forget to read why feof is bad for loop controlling. You will find that in post #2.

8. What do you mean by the actual element of b[z]? I want the output file to show all elements of b[z].

Could you explain in your own words why feof is bad for loop controlling?

9. Yes, you want to send the elements of the array b, but what you send is just the same string every time. You have "b[z]", which is a string!

With feof, the chance of reading the last line twice is high, because the function checks for the EOF indicator and not the stream.

10. Look at this part of the code:
Code:
```int number_ofnumbers=0;
char c;
fscanf(f,"%c", &c);
if (c=='\0')
number_ofnumbers++;

int a[number_ofnumbers];```
This says to read one byte from the file stream, and if it is '\0', create an array "int a[1]", otherwise it will create an array "int a[0]"
What are you trying to do with this part?

11. I want to know how many numbers there are in the file by adding spaces.

12. \0 is the null terminator for strings.
If you have a .txt which is like this :
1
2
3
4
then you could do that by checking for the newline character ('\n')

13. You know what, I think you have done the difficult part, the algorithmic one.

So I made some minutes ago an example of how to copy a file to another. You can found the example here.
If I were you I would try to customize the code found in the example in my needs.

14. Could you look at the algorythm please? The more I look at it, the more doubts I have.

15. The algorithm you used for primes? Isn't this your code? :O

Or are you talking about my 'algorithm' for copying a file to another?