我正在尝试用C语言编写一个程序,该程序读取一个输入文件,该文件包含一个带有时间戳的字符串列表,格式为dd/mm/yyyy-hh:mm:ss,并将这些字符串转换为长整数,然后将这些长整数转换为秒,并计算与2000年的差值。
我编写的两个函数在处理单独程序中手动输入的单个时间戳时可以正常工作,但在阅读文本文件时无法初始化,我也不确定如何使程序迭代整个文本文件以顺序转换每个字符串。
1.文本文件中第一个字符串的输入= 29/11/2021-15:33:56
1.输出文件中预期的第一个输出= 694199036
1.实际输出=从“char *”初始化“char”,从指针生成整数而不进行强制转换[-Wint-conversion]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_FILENAME_LEN 256
#define MAX_STRINGS 100
#define MAX_LENGTH 50
long long timestamp_to_long (const char *timestamp) //converts timestamp to integer
{
// Initialize a long integer variable to store the result
long long result = 0;
// Use a pointer to iterate through the timestamp string
const char *ptr = timestamp;
// Iterate through the timestamp string until the null character is reached
while (*ptr)
{
// If the current character is a digit, add it to the result
if (*ptr >= '0' && *ptr <= '9')
{
result = result * 10 + (*ptr - '0');
}
// Move to the next character
ptr++;
}
return result;
}
long long timeStampToSeconds (long long result) //outputs seconds difference from start date
{
long long startsec = 63115200000; //2000*31557600
long long daysToSec = ((result / 1000000000000) % 100) * (24 * 3600);
long long monthsToSec =((result / 10000000000) % 100) * (24 * 3600 * (365.25 / 12));
long long yearsToSec = ((result / 1000000) % 10000) * 31557600;
long long hoursToSec = ((result / 10000) % 100) * 3600;
long long minutesToSec = ((result / 100) % 100) * 60;
long long secondsToSec = result % 100;
long long totalSec =
daysToSec + monthsToSec + yearsToSec + hoursToSec + minutesToSec +
secondsToSec;
long long difference = totalSec - startsec;
return difference;
}
int main ()
{
char strings[MAX_STRINGS][MAX_LENGTH];
char input_filename[MAX_FILENAME_LEN];
char output_filename[MAX_FILENAME_LEN];
char *p;
int num_strings = 0;
printf ("Enter the name of the input file: ");
scanf ("%s", input_filename);
printf ("Enter the name of the output file: ");
scanf ("%s", output_filename);
// Open the input file
FILE *fileptr = fopen (input_filename, "r");
if (fileptr == NULL)
{
printf ("Error opening file!\n");
return 1;
}
// Read in the strings from the file
while (fgets (strings[num_strings], MAX_LENGTH, fileptr) != NULL)
{
// Remove newline character from the string
int length = strlen (strings[num_strings]);
if (strings[num_strings][length - 1] == '\n')
{
strings[num_strings][length - 1] = '\0';
}
num_strings++;
}
p = (char *) malloc (num_strings + 1);
if (p == NULL)
{
printf ("Unable to allocate memory");
exit (1);
}
// Close the input file
fclose (fileptr);
char timestamp = ("%s", *strings);
// Convert the timestamp to a long integer
long long result = timestamp_to_long (timestamp);
long long difference = timeStampToSeconds (result);
// Open the output file
fileptr = fopen (output_filename, "w");
if (fileptr == NULL)
{
printf ("Error opening file!\n");
return 1;
}
// Write the sorted strings to the file
for (int i = 0; i < num_strings; i++)
{
fprintf (fileptr, "%lld\n", difference);
}
// Close the file
fclose (fileptr);
return 0;
}
1条答案
按热度按时间uxhixvfz1#
因此,我做了一些额外的挖掘,并找到了一个解决方案,我将离开这里,以防有人在未来有类似的问题。
我遇到的主要问题是字符时间戳=(“%s”,* 字符串);这是错误的,通过将其更改为char *timestamps = string [i]并添加一个简单的循环(int i = 0; i〈字符串个数; i++)来迭代整个输入列表。
我还在循环中包含了fprintf函数,以便在每次for循环迭代时写入差异,这是我一开始就应该做的。